So i got everything working, but the way i’ve got it working i’m unsure how to merge the posts together so that I can have this listed A-Z?
Basically what i’m doing is running a query loop to get all of the posts that don’t have my custom taxonomy applied to it.
Then I do a secondary loop to get all of the posts that ARE in my taxonomy, with those posts i check which taxonomy they are a part of and add them as a child. This spits out two lists wonderfully, but it lists the posts and then the cats containing the posts. i.e.
– Product A
– Product C
– Product B (actually a cat)
— Product in Cat 1
Anybody have any clue if I can add a step somewhere to mix them all together to sort them correctly
<?php $terms = get_terms( 'need_cat' );
// convert array of term objects to array of term IDs
$term_ids = wp_list_pluck( $terms, 'term_id' ); ?>
<?php $query1 = new WP_Query( array( 'post_type' => 'product_info', 'order' => 'ASC', 'orderby' => 'title',
'tax_query' => array(
array(
'taxonomy' => 'need_cat',
'terms' => $term_ids,
'field' => 'term_id',
'operator' => 'NOT IN'
)
),
));?>
<?php
$query = new WP_Query( array( 'post_type' => 'product_info',
'tax_query' => array(
array(
'taxonomy' => 'need_cat',
'terms' => $term_ids,
'field' => 'term_id',
'operator' => 'IN'
)
),
));
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>';
$categories = get_terms('need_cat');
foreach ( $categories as $key=>$category ) {
$b = '<li class="productCat toggle">' . $category->name;
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
// print_r($q);
foreach ($q as $key=>$values) {
echo $key;
echo '<ul>';
foreach ($values as $value){
echo '<li>' . $value . '</li>';
}
echo '</ul></li>';
}
?>
<?php if (have_posts()): while ( $query1->have_posts() ) : $query1->the_post();?>
<li>
<a href="<?php the_permalink();?>"><?php the_title();?></a>
</li>
<?php endwhile; endif; ?>