I am trying to set search order by title by using the below code from Relevanssi:
add_filter('relevanssi_modify_wp_query', 'rlv_sort_by_title');
function rlv_sort_by_title($q) {
$q->set('orderby', 'post_title');
$q->set('order', 'asc');
return $q;
}
This is working on the normal search function in wordpress but when using the search function within a view it is not changing it to be ordered by title.
Link to a page where the issue can be seen:
Here is the generic search term: hidden link
Here is the views search term: hidden link
Hi, can you tell me the "Ordering" settings for this View? Go to Toolset > Views and edit this View. If you cannot see the "ordering" settings, scroll to the top right corner of the page and click "Screen options". You can enable the Ordering options here. Try changing this to post_title, ascending.
If that does not change the sort order, add the cached="off" parameter to the View shortcode like this:
[wpv-view name="Your View name" cached="off"]
Let me know the results of these tests.
I have tested turning off the cache but it is still not putting them in alphabetical order on search. The initial load is putting them in alphabetical order. I would like to know what I would have to do for the code that they provide to work with toolset?
Sorry, I didn't get a clear response to this question and it is important:
can you tell me the "Ordering" settings for this View?
I also need to know if you temporarily remove the custom code, is the View sorted by post title as expected?
These two questions will help me determine how to use their custom code effectively with Views.
Sorry I didn't explicitly reply to this because the other part of my answer also answered this. The ordering is set to by "post title" > "ascending" and this causes the page load to work correctly when initially loading the results. Then when you search they display in order of relevance. This is the default method with Relevannsi. But the code I have put above is supposed to fix that. In normal WordPress search (like whole site search results) the archive page with all my posts and custom post types, the results now appear in alphabetical order which is the desired effect, but the view results don't show in alphabetical order...
Okay thanks, I ran some local tests and I'm not seeing the same problem with a basic View, so there must be something else going on.
- Is the search View using AJAX updates? Are the results sorted differently if change to or from AJAX updates?
- Are any other Query filters or search filters applied to the custom search View?
- Is the search results WordPress Archive using pagination? Please enable pagination with AJAX and tell me if the archive sort order is accurate after paginating.