3

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?

0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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