Why is my $query->set('meta_query') not working for a field from the wp_posts table?

202 views Asked by At

I have a field called post_title which is present in the wp_posts table and 2 custom fields - production_year and movie_description - these 2 custom fields are located inside the wp_postmeta table.

I want to filter my posts of custom post type movies on the archive page like: www.mydomain.com/movies?search_text=Superman

This code below in functions.php doesn't work for post_title, it searches only for text in movie_description fields. Is it because $query->set('meta_query') works only for custom post fields from wp_postmeta table and not for standard post fields like post_title from wp_posts table?

functions.php:

<?php
class searchMovies{
 
    function __construct(){
        add_action( 'pre_get_posts', array( $this, 'filterquery' ) );
    }

    function filterquery( $query ){
        global $pagenow;
        global $typenow;

        // THIS DOES NOT SEEM TO WORK HERE ↓↓↓
        if ( is_archive()
            && $query->is_main_query()
            && isset( $_GET['search_text'] )
            && !empty( $_GET['search_text'] )
        ) {
            $query->set(
                'meta_query',
                array(
                    'relation' => 'OR',
                    array(
                        'key' => 'post_title',
                        'value' => $_GET['search_text'],
                        'compare' => 'LIKE'
                    ),
                    array(
                        'key' => 'movie_description',
                        'value' => $_GET['search_text'],
                        'compare' => 'LIKE'
                    )
                )
            );
        }

        return $main_query;
    }
}

new searchMovies();
0

There are 0 answers