I am trying to:
To make the search result page in order by using the toolset custom fields specifically wpcf-featured-search-result custom field
This is the code I made:
function sort_cars_archive_loop($query) {
if ($query->is_search() && $query->is_main_query()) {
$query->set('order', 'DESC');
$query->set('meta_key', 'wpcf-featured-search-result');
$query->set('orderby', 'meta_value_num');
}
}
add_action('pre_get_posts', 'sort_cars_archive_loop');
Link to a page where the issue can be seen:
hidden link
I expected to see:
On the page the post that has green border must be on the top because on pre get posts filter I set them as orderby => meta_value_num based on the meta key of wpcf-featured-search-result
The green border is being set to 1 value.
Instead, I got:
When I put that code on functions.php file it will just say "no item found" it seems when I added the meta key of wpcf-featured-search-result on the add filter function the search query will say "no item found"
What value is saved for this custom field in non-featured posts? If you try to sort a query on a meta_key, posts that have no value for that meta_key will not be included in the results. This causes lots of headaches in Views but it's just how WordPress works. To get around that quirk, you can set a default value for that custom field in every post, or you can use custom code to create a more complex order. Multiple meta keys, for example, might work:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
Look for the section "'orderby' with multiple 'meta_key's" for an example.
The meta_value saved for the meta_key is 1 or 2. The default value is 2. It seems like when I add the pre get post filter. It will return no item found
Your code syntax looks okay to me. I copied the same code into a local site for testing, replaced the custom field slug, and I see results appear on the search archive. So something else must be going on. Can you try these troubleshooting steps?
- Temporarily deactivate all plugins except Types and Views, and comment out any other custom code in your child theme's functions.php file and code snippets. Test again.
- If the problem is resolved, add your other custom code back and reactivate other plugins one by one until the problem returns.
- If the problem was not resolved, may I log in to take a closer look? I will activate private reply fields here so you can share login credentials.
Ok, found the code conflict. It's a snippet I found on another post here that allows search in all custom fields, which I really need. When I remove this code below my code form above allows me to sort by that custom field.
Is there any way to have both pieces of code and avoid this conflict?
if ( is_search() ) {
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter('posts_join', 'cf_search_join' );
Is there any way to have both pieces of code and avoid this conflict?
I'm not exactly sure how that code snippet works to allow custom field searches, and it's not touching any Toolset APIs so it's not something we support here in the forums. The Toolset way to handle ordering for standard search results is to create a WordPress Archive, set the sorting criteria with your custom field, and assign the WordPress Archive to be used for search results. Without any custom code, the usual advice to allow searching custom fields is to use a 3rd-party tool like Relevanssi: https://toolset.com/documentation/user-guides/searching-texts-custom-fields-views-relevanssi/
Sorry, I didn't paste all the code that allows custom field searching, the full code block was submitted here by a user:
https://toolset.com/forums/topic/display-search-results-from-custom-posts/
And I understand that it's not supported in the forums, but I see this request fairly often and it seems like a pretty simple bit of code.
And thanks for the Relevanssi link, I know it's a quality plugin but I don't need all its functionality and I'm trying to keep my plugin usage down, already a lot on this site. Is there no other way to include custom fields in the search results?
Tim
Is there no other way to include custom fields in the search results?
Not with Toolset, no. It's not possible in basic WordPress either without custom code. There are Toolset APIs to modify View queries, but not search (or other) archive queries:
https://toolset.com/documentation/programmer-reference/views-filters
That's to bad. Can this be a feature request? The code provided seems simple enough and works well, unsure why that can't just be rolled into Toolset at some point.
Tim
Of course, but I can't convert this existing ticket to a feature request. Feel free to submit a new ticket here in the forum using the "suggest an improvement" option. Mention that you would like to be able to filter archive queries so you can search for custom field values. Our team will evaluate the request and let you know if it's something we can implement.
My issue is resolved now. Thank you!