Forums › Forums › Search & Filter Pro › Use 'post__in' for 'orderby' is not working
- This topic has 9 replies, 3 voices, and was last updated 8 years, 12 months ago by
Anonymous.
-
Anonymous(Private) May 6, 2016 at 10:46 am #44871
Hello,
I’m trying to use ‘post__in’ as ‘orderby’ method, but that isn’t working for me.
Situation
Based on this: http://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments
I’ve set ‘post__in’ with an array of post id’s that is in a specific order.
I’ve set ‘orderby’ to ‘post__in’ to output the posts in that specific order.I see the values in my array, but it’s not working. I’ve found the problem. In the backend I can set the sort order. If I leave this blank, there are standard values passed.
public 'request' => string 'SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'speelplek' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC LIMIT 0, 10' (length=189)
Is there a way for me to make this work? Thanks in advance!
-Kevin
Ross Moderator(Private) May 6, 2016 at 3:29 pm #44915Hey Kevin
Actually, this is not possible.
S&F uses
post__in
for the query itself – I’ll try to explain:S&F will take a users search parameters, and run a special cache query to get the results.
The results are received as a list of Post IDs, which we use in a WP_Query, in the field
post__in
This means, the post__in field is one of the only fields you cannot use, because S&F will overwrite any posts IDs here with the results it finds.
The best thing to do would be to add a custom field/post meta to your posts, and use this for numbering/ordering – then you can sort the results by this custom field (under the
posts
tab).The logic of this approach is just the same as when using WP pages – you have a field
menu order
where you can specify a numerical value for a posts position.A quick search found: http://wordpress.stackexchange.com/questions/43970/adding-menu-order-column-to-custom-post-type-admin-screen
There are also a number of plugins which allow menu order to be changed simply by “drag and drop”.
I hope that helps!
Anonymous(Private) May 6, 2016 at 3:39 pm #44918Hi Ross,
That’s too bad. I’ll explain my situation:
We’ve made a custom post type with places, and have X & Y coördinates in a custom field. With a special query and function, we get an array of places, ordered by distance (we measure this according to a users current position). This array contains the places in the right order. And because this is not a value stored in the DB but a calculated value, I see no other way of doing this than using ‘post__in’..
Do you see any other way of accomplishing this?
Ross Moderator(Private) May 6, 2016 at 3:46 pm #44922Ah ok, well the solution would be to give you access to the query object after S&F is finished with it, so you can re-order the post IDs..
Let me see if there is a filter I might have forgotten about…
Ross Moderator(Private) May 6, 2016 at 3:54 pm #44924Ah, there is one!
We have a Relevanssi integration, which means in some scenarios we order results by “relevance”…
Anyway, the filter is (yeah this really should be renamed):
sf_apply_filter_sort_post__in
It takes a list of IDs, and returns a list of IDs
You can see an example usage in
public/includes/class-search-filter-third-party.php
add_filter( 'sf_apply_filter_sort_post__in', array( $this, 'relevanssi_sort_result_ids' ), 10, 3);
Hope that helps!
Anonymous(Private) May 6, 2016 at 10:08 pm #44969Ahh, I was running a previous version without the third-party file. Downloaded it.
I’ve tried to make this work, but up until now not succesfully.
Do you mean I can use the ‘relevanssi_sort_result_ids’ function, or should I make my own version of this?I do have an array ready with the right sequence for post__in. But I’m not sure how to make this work with the filter you provided. Could you tell me how to use the function or the idea behind it for a new one?
Thanks in advance!
Ross Moderator(Private) May 10, 2016 at 10:40 pm #45230Hi Kevin
I’ve been working on some bug fixes for the next update.
RE the above, its not a filter I intended for everyone to use (as I need to give more careful thought to the naming of it for example) but indeed it does work.
I just did a test – so, if you place the following in your
functions.php
:add_filter("sf_apply_filter_sort_post__in", "sort_result_ids", 20, 3); function sort_result_ids($result_ids, $query_args, $sfid) { return $result_ids; }
This filter actually does nothing to the result ids – it receives them, and simply returns them – a filter must always return something – in this case it should be an array of IDs.
BTW – by even having this filter in your code, this overrides / ignores any ordering set in the “posts” tab and forces it to order by the order of the IDs in
$results_ids
$result_ids
is just a regular array, with the post IDs – ie –array(12,2342,325,12)
So what you must do, is re-arrange the array, so the IDs are in the order you want. You should not add or remove any IDs, you should really make sure you return the same IDs as what comes into the function – but in any order you like.
Another example, showing the result IDs reversed, which will reverse the order of the results from the first example:
add_filter("sf_apply_filter_sort_post__in", "sort_result_ids", 20, 3); function sort_result_ids($result_ids, $query_args, $sfid) { $result_ids = array_reverse($result_ids); //reverse the array, which reverses the order return $result_ids; }
Hope that helps
Thanks
-
AuthorPosts