Filter and sort result list
-
Do we have ability to sort and filter search result? For example, filter result by pages / pdf / doc / etc? Sort by relevance or date?
Maybe you have values that I can use to rewrite a query? -
Hi @ilocimwca
Yes, of course, all the same values that you're usually using for WP_Query() will work!
You can specify parameters in case you want to call WP_Query() manually or you can use the pre_get_posts hook in case you want to intercept the main query.
What do you actually plan to do?
-
@epsilonadmin I planned pre_get_posts. How to filter to show only pdf or only web pages?
-
Hi @ilocimwca
It's simple to do with this addon
https://fulltextsearch.org/wpfts-addon-files-only-1.0.1.zipIt contains some code to limit post_type to 'attachment' only:
add_action('pre_get_posts', function(&$wpq) { if ($wpq->is_search && $wpq->is_main_query()) { $wpq->set('post_type', array('attachment')); $wpq->set('post_status', array('inherit')); } });Pretty simple. If you want to set up your own post_types, just modify this code. You can read about full WP_Query() parameters at the Wordpress official documentation.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login