WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_query filtering by category and author (2 posts)

  1. TwoThirdsWater
    Member
    Posted 2 years ago #

    I would like to use wp_query to get all posts within a particular category that are by a particular author.

    This would seem to me to be pretty simple, and have created the following code

    $args = array(
    		  'post_type' => 'post',
    		  'posts_per_page' => $numStories,
    		  'cat' => get_post_meta(getSettingsPageID(), "blog_category_id", true),
    		  'byAuthor' => $byAuthor
    		  );
    
    	$temp = $wp_query;
    	$wp_query= null;
    	$wp_query = new WP_Query($args);

    but instead of getting me all posts where both cat AND author match, I get all posts where cat OR author match.

    How do I get BOTH conditions to be matched please?

  2. TwoThirdsWater
    Member
    Posted 2 years ago #

    Idiot Idiot Idiot.

    No sooner do I post it do I see that the filter needs to be 'author' not byAuthor. Couldn't see the wood for the trees.

    For anyone who stumbles across this post, the working code is:

    $args = array(
    		  'post_type' => 'post',
    		  'posts_per_page' => $numStories,
    		  'cat' => $catID,
    		  'author' => $authorID
    		  );
    
    	$temp = $wp_query;
    	$wp_query= null;
    	$wp_query = new WP_Query($args);

Topic Closed

This topic has been closed to new replies.

About this Topic