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 added the following to my functions.php:

add_action('pre_get_posts', 'keyl_get_emp_posts');
function keyl_get_emp_posts($query) {
    if ($query->is_main_query())
        $query->set('post_type', 'employee');
}

and so far it's effectively filtering out the search results. The default widgets Recent Posts and Recent Comments aren't budging, though. What gives?

share|improve this question
add comment

2 Answers 2

pre_get_posts is executed before each and every query. Your $query->is_main_query() is making this code change the query only for the Main query. So, if you're in an archive page, you're modifying only the archive posts, and not any of the others queries (widgets, menus, etc).

But be aware, that code you added there will change the query for ALL your main queries. So if you go to another post type archives (categories, etc) you will be modifying the query to get the post_type = employee

share|improve this answer
    
hm.. so what conditional should i test for to target the "recent posts" and "recent comments" queries then? tia :D –  Ana Ban Jun 12 '12 at 6:48
    
You can try an advanced widget like: wordpress.org/extend/plugins/advanced-recent-posts-widget –  MZAweb Jun 12 '12 at 7:08
add comment

Both the "Recents Posts" widget and the "Recent Comments" widget provide a filter that you can use to alter the query, and they both conveniently take the same argument.

function alter_recent_widget_post_type_wpse_54931($args) {
  $args['post_type'] = 'employee';
  return $args;
}
add_filter('widget_posts_args','alter_recent_widget_post_type_wpse_54931');
add_filter('widget_posts_args','alter_recent_widget_post_type_wpse_54931');

That will alter the widgets every time they run so if you need the widgets to function in multiple contexts you will need something more complicated.

share|improve this answer
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.