Sort search results by custom field
-
Hi there,
Firstly, thanks so much for creating and maintaining a fantastic plugin! It’s easy to use and works great.
My question is, I am wondering if it is possible to sort the results of the plugins’ search, specifically a custom field?
For example I have a a custom post type ‘publications’ and am sorting the archive page using this:
function sort_publication_date_field( $query ) { // do not modify queries in the admin if( is_admin() ) { return; } if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'publications' ) { $query->set('orderby', 'meta_value'); $query->set('meta_key', 'publication_year'); $query->set('order', 'DESC'); } return $query; } add_action('pre_get_posts', 'sort_publication_date_field');
But when I use the search provided by the wp-custom-fields-search plugin, this function does not seem to be called, I expect because it is not using the main search query (?).
So, what I am wanting is to sort/order the search results by their publication_year.
Is there some hook I can use to intercept the wp-custom-fields-search query, or setting I am not seeing in the UI to apply this function?
Thank you in advance.
- The topic ‘Sort search results by custom field’ is closed to new replies.