Home › Forums › Pro Support › Order by Custom Field
- This topic has 9 replies, 2 voices, and was last updated 4 years, 10 months ago by
Tom.
-
AuthorPosts
-
July 4, 2018 at 1:25 am #5108
Ben
ParticipantHi Guys,
Just wondering if I can order the Show Posts results by a Custom Field set up through ACF for a custom post type?
Thanks
BenJuly 4, 2018 at 6:30 pm #5128Tom
KeymasterHey Ben,
You could try this:
[wp_show_posts id="123" settings="orderby=meta_value&order=DESC"]
That’s according to these instructions: https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/
July 12, 2018 at 5:27 pm #5237Ben
ParticipantHi Tom,
Thanks – didn’t see you had posted this, will give it a try.
Thanks,
BenJuly 12, 2018 at 5:56 pm #5239Ben
ParticipantHi Tom,
It works! Once again you’ve come to the rescue – thanks so much, your help is really appreciated.
For anyone else looking to do the same thing, here is the shortcode I ended up with. Needed to add the meta_key to the above code.
Note the meta_key is the name of the Custom Field, in my case it was event_id.
[wp_show_posts id=”16″ settings=”meta_key=event_id&orderby=meta_value&order=ASC”]
Thanks again,
Ben
July 12, 2018 at 7:31 pm #5243Tom
KeymasterGlad you got it working! Thanks for sharing your code 🙂
July 17, 2018 at 6:30 pm #5282Ben
ParticipantHi Tom,
Sorry to bring this up again. I’m having an issue with this method and was wondering if you could shed any light.
The code works OK when I use a WPSP List to show all posts for my CPT (Events)(eg. https://www.orangewinefestival.com.au/event-program/events-program/), but when I choose a taxonomy (event_type) it stops working (eg. https://www.orangewinefestival.com.au/event-program/weekday-events/).
I am using a custom field to sort by, a field I have created called event_id, which is a number field. I tried setting that as a text field but it didn’t fix the issue.
If I change to order by field in WPSP List settings then the order will change, so it appears that when a Custom Taxonomy is being used, the ‘settings’ part of the shortcode is being ignored.
Any ideas?
Thanks,
BenJuly 18, 2018 at 6:35 pm #5297Ben
ParticipantHi Tom,
Further to this, I am having an issue with the sorting on a non-custom field – ID. If you have a look at this page (https://www.orangewinefestival.com.au/event-program/weekend-two-events/), you will see numbers at the bottom of each event (882, 883 etc) these are the ID’s for each Event, using get_the_ID(). Notice that even though I am ordering by ID, set to ASC, it isn’t working.
Note I have removed the ‘settings’ from my WPSP shortcode, so it’s just [wp_show_posts id=”27″ ]
Also when I set the order in WPSP to DESC it changes the order, but it still isn’t correct.
This is getting to be an urgent issue now so any help would be appreciated – thanks.
Ben
July 18, 2018 at 8:19 pm #5301Tom
KeymasterHmm, weird.
Can you try editing the plugin to test something?:
Find this line: https://github.com/tomusborne/wp-show-posts/blob/1.1.3/wp-show-posts.php#L130
And replace it with:
'orderby' => sanitize_text_field( wpsp_get_setting( $id, 'wpsp_orderby' ) ),
Let me know if that works or not.
July 19, 2018 at 4:58 am #5312Ben
ParticipantThanks Tom, that seems to have worked. You’re a lifesaver (again!).
Anything else I should do?
July 19, 2018 at 8:02 pm #5315Tom
KeymasterAwesome! Nope, I’ll get that added by default in the next version 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.