• Here is my WP_Query object:

    $ls_any_posts = new WP_Query(
    		array(
    			'post_type'       => array('post', 'event', 'deal', 'contest', 'partner'),
    			'posts_per_page'  => 5,
    			'meta_query'      => array(
    		            array(
    		                'key'     => 'whats_hot',
    		                'value'   => 1,
    		                'compare' => '=',
    		            )
    	                ),
    	                'post_status'     => array('publish', 'future'),
    	       )
    	);

    Currently, this queries for all my custom post types that have a status of Publish or Future.

    Instead, I only want to query “Future” posts if the post type is “Event”. If it’s one of the other post types, the status must be “Publish”.

    How could I accomplish that?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Use below code may be it will help you

    <?php query_posts('posts_per_page=10&post_status=future&post_type=event'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <span class="datetime"><?php the_time('j. F Y'); ?></span></p>
    <?php endwhile;
    else: ?><p>No future events scheduled.</p>
    <?php endif; ?>

    Thread Starter CoBu1

    (@cobu1)

    Thanks for the suggestion, but that doesn’t answer my question, and isn’t query_posts() a bad practice?

    I want my “Future” events in the same feed as my other “Published” post types, but I do not want “Published” events or “Future” posts of other types.

    Moderator bcworkz

    (@bcworkz)

    You cannot accomplish that with one WP_Query, you’d have to do two separate ones, then merge the results. Use usort() to get the dates in the proper order. Another complication is you’d have to manage the pagination yourself because it cannot be known how many posts from each query would make up a full page. You also would need to keep track of what the last post of each query that actually made it onto the page is, in order to know where to offset into the next page of results.

    Another option would be to keep the current query and weed out unwanted posts in the loop, but you still have the same pagination issues.

    There might be some way to manage this with an elaborate mySQL query sent using a $wpdb method, but I wouldn’t know where to even begin with constructing that one. Doing so could solve the pagination issues.

    query_posts() is considered bad practice because it discards the main query and runs a new one. Such a waste! If you’re ignoring the main query anyway, it’s a waste either way. Ideally, alter the main query through ‘pre_get_posts’, then no query is wasted and you get the results you want (if possible ?? )

    We should also try to get away from query argument strings. Arrays offer more flexibility, are easier to read, are less error prone, and more ‘modern’. If not for the need of reverse compatibility, argument strings would have been done away with.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Querying multiple post types with different post statuses’ is closed to new replies.