WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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;

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

Your Answer


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.