sort posts inside a query

Hello,
What's the best way to sort posts inside a query? I have little programming knowledge so I have no clue what to do.

This is the code I'm using:

<?php $args = array('tax_query' => array(array('taxonomy' => 'post-status','field' => 'slug','terms' => array ('post-status-published')))); $query = new WP_Query( $args );?>
    <?php if ( $query->have_posts() ) : $duplicates = []; while ( $query->have_posts() ) : $query->the_post(); ?>

<?php endwhile; wp_reset_postdata(); endif; ?>

What I would like is if posts are in a category called 'Major' to be at the top. For example, if my posts are like this:
- Post
- Post
- Post (major)
- Post (major)
- Post
- Post (major)

I would like it be like the following:
- Post (major)
- Post (major)
- Post (major)
- Post
- Post
- Post

I would also like to surround posts in the group with a div like:
<div>
- Post (major)
- Post (major)
- Post (major)
</div>

I've tried so many things that I can't provide examples because it will go on. I guess what I think needs to be done is to put everything inside the group and use "foreach" to style everything else, not before the query because there are going to be posts not using that group.

Thanks

  • Nastia
    • Support Rock Star

    Hello Gregory

    I hope you are doing well today!

    Unfortunately, I am not a PHP expert, so I have asked a second level support to provide you a feedback with this.

    The developer response might require more time compare to the support staff, because they are dealing with really complex tasks.

    Have a nice day and take care!

    Cheers,
    Nastia

  • Panos
    • SLS

    Hi Gregory ,

    Glad you found a way!

    Hope you don't mind another opinion and definitely hope I don't confuse you :slight_smile:

    I don't think this is possible with WordPress query functions, so I think you need a custom quey here for this kind of select.

    Here is a query you may like to try out:

    global $wpdb;
    	$query = "(
    		SELECT * FROM $wpdb->posts
    		LEFT JOIN $wpdb->term_relationships ON
    		($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    		LEFT JOIN $wpdb->term_taxonomy ON
    		($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    		WHERE $wpdb->posts.post_status = 'publish'
    		AND $wpdb->term_taxonomy.taxonomy = 'category'
    		AND $wpdb->term_taxonomy.term_id = 39
    		ORDER BY post_date DESC
    		)
    		UNION ALL(
    		SELECT * FROM $wpdb->posts
    		LEFT JOIN $wpdb->term_relationships ON
    		($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    		LEFT JOIN $wpdb->term_taxonomy ON
    		($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    		WHERE $wpdb->posts.post_status = 'publish'
    		AND $wpdb->term_taxonomy.taxonomy = 'category'
    		AND $wpdb->term_taxonomy.term_id <> 39
    		ORDER BY post_date DESC
    		)
    	";
    
    	$results = $wpdb->get_results($query);

    It hasn't been tested properly so it might need some corrections! Hope this helps a bit :slight_smile:

    Thanks!
    Panos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.