Skip to content

Instantly share code, notes, and snippets.

@SuryawanshiPrajakta
Last active May 26, 2021 17:28
Show Gist options
  • Save SuryawanshiPrajakta/9f075dee951ea376c706bb2d0613d2e7 to your computer and use it in GitHub Desktop.
Save SuryawanshiPrajakta/9f075dee951ea376c706bb2d0613d2e7 to your computer and use it in GitHub Desktop.
Order posts by custom field value
1) Create a custom field and attach it to the post type require - https://cl.ly/dc507435d638
2) Add the below code in the active child theme's functions.php file -
add_filter( 'uael_posts_query_args', function( $query, $settings ) {
// Modify the query here
$query = array(
'meta_key' => 'post_order',
'orderby' => 'meta_value',
'order' => 'DESC'
);
return $query;
}, 10, 2 );
This code will order the posts according to the value of the custom field.
3) Please replace the 'orderby' value with the the field name and the 'order' with the required order.
@protasisimone
Copy link

Thanks for ur snippet...Will this code modify all widget queries? Or how do I specify which widget should hit?

@SuryawanshiPrajakta
Copy link
Author

Hello,

This code will affect the Ultimate Addons for Elementor plugin's Posts widget.

Please let me know if you have any further queries.

@alithedvlpr
Copy link

@SuryawanshiPrajakta thanks for the solution. But I have a query, let's say if I have the same widget on more than one page then this will modify the query for each page. So is there any way to modify a particular page widget?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment