• I have a website where I have a product page, where are all the products with categories and products posts which have no categories.

    I need all the categories and products sorted by a name (ASC order), so all of them will be output in correct order and not like categories comes then comes products.

    Issue is I dont know how to do that. I could be able to combine both arrays. But how to I output them, if one needs foreach loop and other needs while loop.

    I have figured out how to output all posts with no categories and all the categories.

    Here’s my code:

    
    <?php 
    
    	// post array
    
    	$posts = new WP_Query( array(
    		'post_type' => $post_type,
    		'tax_query' => array( array(
    			'taxonomy' => $taxonomy,
    			'field' => 'term_id',
    			'operator' => 'NOT IN',
    			'terms' => get_terms( $taxonomy, array('fields' => 'ids') )
    		))
    	)); 
    
    	$args = array(
    		'parent' => 0,
    		'hide_empty' => true,
    		'exclude' => 13,
    	);
    
    	// products categories array
    
    	$products = array(
    		'tax_query'	=> array( array(
    			'taxonomy' => $taxonomy,
    			'terms' => get_terms($taxonomy, $args)
    		))
    	);
    ?>
    

    Loop must output each product/category name, link and featured image. I’m using Category Images plugin to output category featured image.

    Output should look like this:
    1. A (is_post)
    2. B (is_term)
    3. C (is_post)
    4. D (is_post)
    5. E (is_term)
    6. F (is_term)

    • This topic was modified 7 years, 9 months ago by Imre.
    • This topic was modified 7 years, 9 months ago by Imre.
    • This topic was modified 7 years, 9 months ago by Imre.
    • This topic was modified 7 years, 9 months ago by Imre.
    • This topic was modified 7 years, 9 months ago by Imre.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Unfortunately, WP_Query does not support ordering by taxonomy terms. Meta values is about as close as it gets. I previously had worked up a custom query to order by category names:

    //list all posts & portfolios w/ categories ordered by category name
    global $wpdb, $post;
    $query = $wpdb->get_results( "SELECT * FROM $wpdb->posts AS p
    	LEFT JOIN $wpdb->term_relationships AS r ON (p.ID = r.object_id)
    	INNER JOIN $wpdb->term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
    	INNER JOIN $wpdb->terms AS t ON (r.term_taxonomy_id = t.term_id)
    	WHERE p.post_type IN ('post', 'portfolio') 
    	AND p.post_status = 'publish'
    	AND x.taxonomy = 'category'
    	ORDER BY t.name ASC, p.post_date DESC" 
    );

    You should be able to adapt it to your needs.

    Thread Starter Imre

    (@jack-w)

    I forgot to clarify my problem correctly. So let me explain a bit more.

    I have custom post type called products and taxonomy called categories. Some products have taxonomies terms and child terms (Product categories and subcategories). Some products do NOT have taxonomies at all, but still are in products post type. My goal is to output ONLY taxonomy terms (only categories and NOT subcategories) and custom post with NO taxonomy. They must be sorted by alphabetical order, since outputted objects use the same HTML elements with the same CSS classes.

    Back to your code example. I changed them in your code example and created a loop to output them. Problem was that it didn’t show posts with no taxonomy.

    Why didn’t this query show posts with no taxonomy. I tried removing this line of code:
    AND x.taxonomy = 'categories'

    But still didn’t show posts with no category. Unfortunately I’m not that skilled with WordPress and PHP. Could you help me out?

    • This reply was modified 7 years, 9 months ago by Imre.
    • This reply was modified 7 years, 9 months ago by Imre.
    Moderator bcworkz

    (@bcworkz)

    OK, I misunderstood, my code will not help you. I’m unaware of any way to sort by two unrelated columns at the same time with SQL. Once you get everything into a single array with PHP, you need to use usort() to manage the sorting. The sorting callback can get the appropriate data from either location and return it’s determination.

    Thread Starter Imre

    (@jack-w)

    Once I get them into single array and use usort, how do I output them. Is it possible to out post and terms with one loop? If yes then which loop should I use?

    Moderator bcworkz

    (@bcworkz)

    Yes, a single loop, as everything is in one array now. Use a normal foreach loop, since the while( have_posts()) sort of loop only works for posts and you have mixed content.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Ordering Posts and Terms in ASC order’ is closed to new replies.