Last active
June 27, 2022 09:07
-
-
Save Alimir/edb3902bbc74767e17fffa00c3f8b31f to your computer and use it in GitHub Desktop.
Filter posts by like amount in elementor post widget
This file contains 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
<?php | |
add_action( 'elementor/query/order_by_like_amount', function( $query ) { | |
// Get current meta Query | |
$meta_query = $query->get( 'meta_query' ); | |
// If there is no meta query when this filter runs, it should be initialized as an empty array. | |
if ( ! $meta_query ) { | |
$meta_query = []; | |
} | |
// Append our meta query | |
$meta_query[] = array( | |
'relation' => 'OR', | |
array( | |
'key' => 'like_amount', | |
'compare' => 'NOT EXISTS' | |
), | |
array( | |
'key' => 'like_amount', | |
'compare' => '>=', | |
'value' => 1, | |
'type' => 'NUMERIC' | |
) | |
) ; | |
$query->set( 'meta_query', $meta_query ); | |
$query->set( 'orderby', array( 'meta_value_num' => 'DESC', 'date' => 'DESC' ) ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment