I've been trying to create a Recent Post widget where it only show/display posts that have both categories X and Y (or more). The categories X and Y is user-defined, so I'm planning to make it look like this:

enter image description here

In fact, that picture is an actual screenshot from what I have managed to create.

It is working fine if I select only from one category. It would show all posts which have the category of X (or X's children). However, if I add one more category, it would stop working--instead, the widget would show all recent posts regardless of categories.

enter image description here

For creating this code, basically I copied the widget_recent_posts class from default-widget.php and add a field for ID input. It utilizes WP_query to query the posts and category__and to select the post:

$r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'category__and' => array($category_name), 'ignore_sticky_posts' => true ) ) );

What I have created so far (the full class) is available here: http://codepad.org/yf1ZFAt5

I'd be really thankful if anyone could hep me, as I have been messing around with this one for hours but haven't managed to tackle it. :s

1 Answer 1


you're using 'category__and' => array($category_name) which according to the input becomes 'category__and' => array('594,3')

Here '594,3' is not a valid id

instead try 'category__and' => explode(',', $category_name)

This will make the input as 'category__and' => array('594', '3')

  • Hmm... with 'category__and' => explode(',', $category_name), it doesn't show anything at all (the whole widget, including the widget title, seems to be gone)... while I tried with 'category__and' => array(explode(',', $category_name)), it shows posts from the Uncategorized category. :s Should this be put on WP_Query (when querying the post) or on other places?
    – deathlock
    Commented Oct 22, 2012 at 21:17
  • I have just realized that category__and does not display child categories... I guess this is why using category__and shows nothing, because most of all posts are categorized under 594's children, not the category 594 itself... oddly it did show the children when I was experimenting back then though. :| Now I guess my question changes to... how is it possible to make category__and always includes children?
    – deathlock
    Commented Oct 23, 2012 at 0:33
  • AFAIK it's not possible to do that directly. You'll have to query the ID's of child categories once & then put them all in category__and Commented Oct 23, 2012 at 7:53
  • Hmm, in the end it gets complicated... and I guess I'd go for a cat instead :s Thanks anyway!
    – deathlock
    Commented Oct 24, 2012 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.