We're merging with GenerateBlocks! Learn more here.

[Resolved] Order by Custom Field

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Order by Custom Field

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #5108
    Ben
    Participant

    Hi 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
    Ben

    #5128
    Tom
    Keymaster

    Hey 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/

    #5237
    Ben
    Participant

    Hi Tom,

    Thanks – didn’t see you had posted this, will give it a try.

    Thanks,
    Ben

    #5239
    Ben
    Participant

    Hi 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

    #5243
    Tom
    Keymaster

    Glad you got it working! Thanks for sharing your code 🙂

    #5282
    Ben
    Participant

    Hi 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,
    Ben

    #5297
    Ben
    Participant

    Hi 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

    #5301
    Tom
    Keymaster

    Hmm, 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.

    #5312
    Ben
    Participant

    Thanks Tom, that seems to have worked. You’re a lifesaver (again!).

    Anything else I should do?

    #5315
    Tom
    Keymaster

    Awesome! Nope, I’ll get that added by default in the next version 🙂

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.