Forums › Forums › Search & Filter Pro › Sorting of results of custom post type / ACF
Tagged: acf, custom post types, sorting
- This topic has 14 replies, 3 voices, and was last updated 9 years, 11 months ago by Anonymous.
-
Anonymous(Private) July 12, 2014 at 5:49 pm #2425
Hi Ross,
I am testing and playing around with the plug in, so far all is good (congrats for your work btw).
I am having one issue though, I cannot get the results to be sorted the way I want them (or any way at all for that matter). The default sorting seems to be by posting date, DESC. No matter what I try I cannot change that.
I am using this plugin to filter/manipulate a table of custom posts created by ACF. I would like to achieve a sorting by a custom date (meta called “race_date”) which was created with date picker field in ACF. I would also like to allow the user to sort by title or other fields.
I tried adding:
function order_results_default( $query ) { global $sf_form_data; if ( $sf_form_data->is_valid_form() && $query->is_main_query()) { //order results $query->set('orderby', 'race_date'); $query->set('meta_key', 'race_date'); $query->set('order', 'DESC'); } } add_action( 'pre_get_posts', 'order_results_default' );
at the functions.php (and a few variations of the code) but nothing worked.
Adding a “sort order” field doesn’t help either, since it is not seem to be working.
could you please help me out with this one as it is kind of important?
thanks
Ross Moderator(Private) July 13, 2014 at 9:01 pm #2448Hey Shirley it sounds like there may be some other code or plugin modifying the sort order so that no matter what this plugin sets its being overwritten..
The code you are using above need a little modification though, as the later versions of the plugin uses a different priority for
pre_get_posts
so the code above should look like:function order_results_default( $query ) { global $sf_form_data; if ( $sf_form_data->is_valid_form() && $query->is_main_query() && !is_admin()) { //order results $query->set('orderby', 'race_date'); $query->set('meta_key', 'race_date'); $query->set('order', 'DESC'); } } add_action( 'pre_get_posts', 'order_results_default', 21 );
I also added an
is_admin
check which is always good practise when using pre_get_posts.Try the above and let me know, and if its still not working out for you send me a link so I can take a better look 🙂
Thanks
Ross Moderator(Private) July 14, 2014 at 1:34 pm #2466Hey Shirley, you can reply here with a private message with your wp-admin login details, or you can use our contact form and they will be emailed to me:
Ross Moderator(Private) July 15, 2014 at 12:27 am #2507Hi Shirley
Its an unusual problem I have to say, other parameters work fine but not this one :/
Can you try the plugin on a dev/clean install, or disable other plugins and see if they are interfering? I have a feeling that another plugin or script is changing this so no matter what you choose it is ignored – this makes sense as other filtering options seem to work fine…
Let me know when you have done this and if there are still problems we can move on to the next step.
To change the number of results – this can be found under
Settings -> Reading
🙂Thanks
Anonymous(Private) July 15, 2014 at 4:43 pm #2531Hi Ross,
Before going on and disabling plugins (it’s a bit of a pain), I did the following case analysis for you, which I actually think will help you understand what is going on.
By “hard-coding” I mean placing the code above on my functions.php.
In a nutshell, when I don’t place the code on functions.php, sorting on demand works fine, but when I place it, it always does the sorting according to waht code says and it then ignores the user-input “sort by”.
I can see two possible solutions for this (don’t know how to implement them though):
1) Somehow the “Sort by Date ASC” is pre-selected and we actually don’t allow the “sort by” to have a blank value.
2) The script on functions.php is executed only when the “sort by” value is blank
---------------- CASES ------------------- A: Without hard-coding on functions.php No dates selected, no sort by selected, just submit: Sorts by publish date, DES No dates selected, choose sort by date ASC: Works No dates selected, choose sort by date DES: Works Date range selected, no sort by selected, just submit: Sorts by publish date, DES Date range selected, choose sort by date ASC: Works Date range selected, choose sort by date DES: Works B: Sort by date ASC, hard-coded on functions.php No dates selected, no sort by selected, just submit: ASC No dates selected, choose sort by date ASC: ASC No dates selected, choose sort by date DES: ASC Date range selected, no sort by selected, just submit: ASC Date range selected, choose sort by date ASC: ASC Date range selected, choose sort by date DES: ASC
I really hope that helps, if not, let me know and I will then start disabling plugins and test again…
best
S.
Ross Moderator(Private) July 16, 2014 at 1:51 pm #2561Hey Shirley, I think I get what you are saying… so basically, the code you place in functions.php should on come in to effect if there is no sorting option selected – I have an idea to modify the code above to do this check so bear with so I can find some time to do some tests.
Thanks
-
AuthorPosts