How can I modify the query by adding to the existing query?

Question

I have two functions that both modify the query, however the most recent one I’ve created, I would like to add this modification alongside the previous one (i.e. not overwriting it).

The idea is that this function would run last, and essentially be added as an ‘AND’ meta_query. But how can I get it to do that?

// Order any 'featured' psychics to be at the top
function pr_psychic_featured_pinned($query) {

    if(!is_admin() && $query->is_post_type_archive('psychic')) {

        // Get current meta query
        $meta_query = $query->get('meta_query');

        // If there is no meta query when this filter runs,
        // it should be initialised as an empty array
        if(!$meta_query) {
            $meta_query = [];
        }

        // Append our meta query
        $meta_query = [
            'key'       => 'psychic_featured',
            'orderby'   => 'meta_value',
            'order'     => 'DESC'
        ];
        $query->set('meta_query', $meta_query);

    }

    return $query;

}
add_action('pre_get_posts', 'pr_psychic_featured_pinned');

0
Lee 4 weeks 2021-09-22T10:23:51-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse