Home › Forums › Pro Support › Sort By Custom Field Value
Tagged: custom fields, order by, sort
- This topic has 15 replies, 4 voices, and was last updated 1 year, 10 months ago by
elvin.
-
AuthorPosts
-
February 15, 2019 at 11:38 am #7796
Clayton
ParticipantHey 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.
February 15, 2019 at 5:47 pm #7803Tom
KeymasterHi 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 🙂
February 19, 2019 at 6:42 am #7840Clayton
ParticipantAmazing! Worked like a charm. Thanks! Loving the continued development on WP Show Posts.
February 19, 2019 at 7:10 pm #7843Tom
KeymasterAwesome! Glad I could help 🙂
November 15, 2021 at 7:52 pm #35485Ben
ParticipantHi 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,
BenNovember 15, 2021 at 8:00 pm #35491Ben
ParticipantCan’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
November 15, 2021 at 8:34 pm #35495elvin
ModeratorHi 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?
November 15, 2021 at 9:21 pm #35498Ben
ParticipantThanks Elvin, already running 5.8.2, will disable all plugins and get back to you.
November 15, 2021 at 9:34 pm #35501elvin
ModeratorIf 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?
November 16, 2021 at 3:43 am #35516Ben
ParticipantSo in the Classic block, it doesn’t get converted but the orderby isn’t working either.
November 16, 2021 at 3:57 am #35519Ben
ParticipantI’m using the Alpha version of WP Show Posts 1.2.0 Alpha3 too.
November 16, 2021 at 4:33 pm #35551elvin
ModeratorIf 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.
November 16, 2021 at 4:42 pm #35554Ben
ParticipantThanks 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…
November 16, 2021 at 5:29 pm #35559elvin
ModeratorWe 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; } );
November 16, 2021 at 6:03 pm #35564Ben
ParticipantThanks 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.
-
AuthorPosts
- You must be logged in to reply to this topic.