Last active
May 26, 2021 17:28
-
-
Save SuryawanshiPrajakta/9f075dee951ea376c706bb2d0613d2e7 to your computer and use it in GitHub Desktop.
Order posts by custom field value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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?