We're merging with GenerateBlocks! Learn more here.

[Resolved] Sort By Custom Field Value

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Sort By Custom Field Value

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #7796
    Clayton
    Participant

    Hey guys,

    I’m trying to make WP Show Posts sort by a custom field numerical value. Is that possible? I can create a custom field for the custom post type I’ve already created. I just need to be pointed in the right direction on the actual execution of sorting post lists by a custom fields value.

    Any help would be greatly appreciated!

    Running the latest version of GP Premium and WP Show Posts Beta.

    #7803
    Tom
    Keymaster

    Hi there,

    Try adding this to your shortcode:

    [wp_show_posts settings="meta_key=your_custom_field&orderby=meta_value_num&order=ASC"]

    Let me know 🙂

    #7840
    Clayton
    Participant

    Amazing! Worked like a charm. Thanks! Loving the continued development on WP Show Posts.

    #7843
    Tom
    Keymaster

    Awesome! Glad I could help 🙂

    #35485
    Ben
    Participant

    Hi Guys,

    I’m using this shortcode to sort my posts by a custom field and it works well, except WordPress is converting the & to html character & (and amp semi-colon) each time I edit the page resulting in the code not working – is there a way to stop this?

    ie.

    [wp_show_posts id="213" settings="meta_key=time&orderby=meta_value_num&order=ASC"]

    Just realised this forum is doing the same thing so I can’t show the example…

    Thanks,
    Ben

    • This reply was modified 1 year, 10 months ago by Ben. Reason: correct spelling
    • This reply was modified 1 year, 10 months ago by Ben.
    • This reply was modified 1 year, 10 months ago by Ben.
    #35491
    Ben
    Participant

    Can’t seem to edit the post again, here is a screen grab example:

    https://www.dropbox.com/s/se6wbif8fw65ll3/wp-show-posts.png?dl=0

    #35495
    elvin
    Moderator

    Hi Ben,

    Can you try updating WordPress to the latest version (5.8.2) and disable ALL plugins except WPSP and try again if the shortcode is being escaped?

    #35498
    Ben
    Participant

    Thanks Elvin, already running 5.8.2, will disable all plugins and get back to you.

    #35501
    elvin
    Moderator

    If it’s still being escaped, can you try placing the shortcode on a Classic block instead of a Shortcode block and see if it works?

    #35516
    Ben
    Participant

    So in the Classic block, it doesn’t get converted but the orderby isn’t working either.

    #35519
    Ben
    Participant

    I’m using the Alpha version of WP Show Posts 1.2.0 Alpha3 too.

    #35551
    elvin
    Moderator

    If the orderby isn’t working, it’s possible that the time meta key slug doesn’t exist or it’s returning a string value.

    Can you tell us how you’ve created the meta field? If you’re using the date, you should use:

    [wp_show_posts id="213" settings="orderby=date&order=ASC"]

    Or just do the sorting on the list’s WPSP “More settings” tab if this is a single use post list.

    #35554
    Ben
    Participant

    Thanks Elvin, I created a custom field using ACF. It’s a number field. Unfortunately I can’t use date as ‘time’ is the length of time of a bike ride, see the output of the WP Show Posts here (https://rideorange.com.au/routes/).

    I tried using a filter to do the same following code examples in the forum but I couldn’t get it to work. This shortcode works but every time I edit the page i’ll need to correct the shortcode settings to make sure it keeps working which isn’t the end of the world, just not ideal…

    #35559
    elvin
    Moderator

    We can do a workaround by stripping the attributes of the shortcode and applying the query to through a filter instead.

    Use only the base – [wp_show_posts id="213"]

    And then filter with something like this –

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if ( 213 === $settings['list_id'] ) {
            $args['meta_query'] = array(
                array(
                    'key' => 'time',
                    'orderby' => 'meta_value_num',
                    'order' => 'ASC',
                )
            );
        }
    
        return $args;
    } );
    #35564
    Ben
    Participant

    Thanks Elvin, unfortunately this code just blanks the page (except for the header).

    I tried similar code previously and had the same problem each time – no content below the header.

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