Custom Search width post and postmeta
-
Hello,
I need a custom search query because I use al lot of custom fields in a custom post type. I created a function cas_custom_search_query($query) and set all the attributes I needed for the search with the set-function. They are added to the standard-search-query with title and content:function cas_custom_search_query( $query ) { if ( !is_admin() && $query->is_search) { $query->set('post_type', array('book', 'edition')); $query->set('meta_query', array( 'relation' => 'OR', array( 'key' => 'author_artist', 'value' => $query->query_vars['s'], 'compare' => 'LIKE' ), array( 'key' => 'sub_title', 'value' => $query->query_vars['s'], 'compare' => 'LIKE' ), array( 'key' => 'author_contributors', 'value' => $query->query_vars['s'], 'compare' => 'LIKE' ) ));
Because this query is only nearly perfect (it creates a essescial “AND” instead of an “OR” in my SQL query), I had to made my own SQL, joining the postmeta-table and searching the search-string in all relevant fields.
I know that I can choose the relation above as ‘OR’ but this is not the problem. The AND appears between the string-search in title OR content AND in sub-title, author_artist, etc.Now I can output the posts with
$search_results = $wpdb->get_results($sql_query);
but I cannnot sort or paginate my result list, because this normally is made with a WP_query object.
For example with:twentythirteen_paging_nav($wp_query);
My question is:
1) is there a possibility to manipulate the sql-query created by query arguments like mentioned above (changing an OR to an AND)
2) how can I create the $query object with a self made SQL-query in order to sort and paginate the query object?
3) I also will need a fourth field in my SQL query ‘publication_date’ just in order to sort the posts by this field. Do you have an idea how I can get this also running?I guess, this is not trivial. But I’m sure there is a solution in WordPress width on-board means for this.
Thank you for any help,
Cat.
- The topic ‘Custom Search width post and postmeta’ is closed to new replies.