wp query – Custom meta_query order for Elementor based on post meta key

Question

I’m failing at writing an Elementor $meta_query function so posts will be ordered by meta_value_num of a given key.

Each post has a numeric value for the key google_unique_page_views

My example is below:

// Custom query to order 'recommended reading' posts by populatrity
add_action( 'elementor/query/my_custom_filter', function( $query ) {
    $meta_query = $query->get( 'meta_query' );
    if ( ! $meta_query ) {
        $meta_query = [];
    }
    $meta_query[] = [
        'order'=> 'DESC',
        'key' => 'google_unique_page_views',
        'orderby' => 'meta_value_num',
    ];
    $query->set( 'meta_query', $meta_query );   
});

Do I need to include the value query?

0
JVAN 4 weeks 2021-11-03T18:43:53-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse