WordPress.org

Ready to get started?Download WordPress

Forums

WP_Query - include one category that does not also appear in another category (2 posts)

  1. madivad
    Member
    Posted 2 years ago #

    I want to use WP_Query and construct two queries that:

    1. show posts from TWO categories (eg: current category + ontop category)
    2. show posts from only the current category (ie, it can't appear in the "ontop" category)

    Untested, but I think I can go about it like this:

    // The query
    $category = get_the_category();
    $the_query = new WP_Query(
    		array( 'category__and' => array( $category, 286 ) )
    		); // where 286 is the "ontop" category
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	do_old_loop();
    endwhile;
    
    // The second query
    $the_query = new WP_Query(
    		// This is my main question, how to include the query for this.
    		// I want to include the current category, but exclude the category 286
    		); // where 286 is the "ontop" category
    
    // The second loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	do_old_loop();
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    • The do_old_loop function has the makeup of how the loop was originally constructed--I just wrapped it in a function so I could call it again (*the function just remains in the loop.php right below this code - is that ok? bad practise?
    • Should the reset post data appear after each loop has run?
    • how do I construct the second query???
    • This is where I am stuck, I can't work out how to create this. The reference at http://codex.wordpress.org/Class_Reference/WP_Query includes a lot of examples, but not this one.

  2. madivad
    Member
    Posted 2 years ago #

    ok, so this doesn't work. not at all... I have the right category code for the ontop post, but when I click the page, nothing displays. I understand the second part isn't implemented yet, but I thought I would see the ontop posts at least... Any help appreciated

Topic Closed

This topic has been closed to new replies.

About this Topic