Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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')

share|improve this answer
    
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 Oct 22 '12 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 Oct 23 '12 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 –  Mridul Aggarwal Oct 23 '12 at 7:53
    
Hmm, in the end it gets complicated... and I guess I'd go for a cat instead :s Thanks anyway! –  deathlock Oct 24 '12 at 17:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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