Order WP_Query by meta and post date

I’m currently working on a small blog which has a sidebar that displays all “special projects”.

“Special projects” is just a category, the sidebar will only display 4 posts at a time which are filtered by post date, but I also have a custom meta box to allow the user to feature posts.
These featured post should show up on the top of the special projects.

Read More

Right now my query is like this:

new WP_Query("showposts=" . $instance['num'] . "&cat=" . $instance["cat"] . "&order=ASC&order_by=date")

And I can get the featured meta data like this:

 $featured = get_post_meta($single_cat_post->ID, 'soy_featured_post', true);

But how could I integrate this inside the WP_Query?

Related posts

Leave a Reply

1 comment

  1. First of all, ‘showposts’ has been replaced by ‘posts_per_page’ when using WP_Query. I’ve corrected that in your code. Also, within a loop you should just be able to use $post->ID instead of $single_cat_post->ID.

    I would use two loops. Set your paramaters, then in the first loop include a condition to check for the meta value, reset the query, then do another loop and include a condition that checks for the meta value and outputs nothing if it exists.

    In the first query, I added a check to see how many posts are returned by the first loop. Then using that value (subtracted by 4) I calculated a variable to use for posts_per_page in the second loop. Then I added a conditional to only run the loop if the result is greater then 0.

    This is untested, but it should work or at least put you on the right path!

    <?php 
    $args = array(
        'posts_per_page' => 4,
        'meta_key'  => 'soy_featured_post',
        'cat' => $instance["cat"],
        'orderby' => 'date',
        'order' => 'ASC'
    );
    
    $special_post_query = new WP_Query( $args );
    $special_posts_found = $special_post_query->found_posts;
    
    if ($special_post_query->have_posts()) :
        while( $special_post_query->have_posts() ) : $special_post_query->the_post(); 
    
        // POST WITH META VALUE OUTPUT
        the_title();
    
        endwhile;
    endif;
    
    wp_reset_query();
    
    $second_loop_posts_per_page = 4 - $special_posts_found;
    
    if ($second_loop_posts_per_page > 0) {
        $args = array(
            'posts_per_page' => $second_loop_posts_per_page,
            'cat' => $instance["cat"],
            'orderby' => 'date',
            'order' => 'ASC'
        );
    
        if ($special_post_query->have_posts() ) :
            while( $special_post_query->have_posts() ) : $special_post_query->the_post(); 
    
            // Condition to test for NO meta value
            if (get_post_meta($post->ID, 'soy_featured_post', true) == null) {
                // CODE
                the_title();
            } else {
                // Don't print anything because the meta value exists
            }
    
            endwhile;
        endif;
    
        wp_reset_query();
    } ?>