1

If I use the following code, it is working:

new WP_Query([
            'post_status'    => 'publish',
            'post_type'      => "some_custom_post_type",
            'orderby'        => 'DATE',
            'order'          => 'DESC',
            'facetwp'        => true,
            'posts_per_page' => 15,
            'paged'          => $paged,
            '_meta_or_title'=> $_POST['query'],
            'meta_query'    => array(
                'relation'      => 'OR',
                array(
                    'key'       => 'some_meta_value1',
                    'value'     => $_POST['query'],
                    'compare'   => 'LIKE',
                ),
                array(
                    'key'       => 'some_meta_value2',
                    'value'     => $_POST['query'],
                    'compare'   => 'LIKE',
                ),
            )
        ])

But whenever I use tax_quey, it is not working, e.g.:

new WP_Query([
                'post_status'    => 'publish',
                'post_type'      => "some_custom_post_type",
                'orderby'        => 'DATE',
                'order'          => 'DESC',
                'facetwp'        => true,
                'posts_per_page' => 15,
                'paged'          => $paged,
                'tax_query'      => [[
                   'taxonomy' => 'some_taxonomy',
                   'terms'    => ['some_terms_slug'],
                   'field'    => 'slug',
                   'operator' => 'IN',
                ]],
                '_meta_or_title'=> $_POST['query'],
                'meta_query'    => array(
                    'relation'      => 'OR',
                    array(
                        'key'       => 'some_meta_value1',
                        'value'     => $_POST['query'],
                        'compare'   => 'LIKE',
                    ),
                    array(
                        'key'       => 'some_meta_value2',
                        'value'     => $_POST['query'],
                        'compare'   => 'LIKE',
                    ),
                )
            ])

Meta query modifier code

add_action( 'pre_get_posts', function( $q )
{
    if( $title = $q->get( '_meta_or_title' ) )
    {
        add_filter( 'get_meta_sql', function( $sql ) use ( $title )
        {
            global $wpdb;

            // Only run once:
            static $nr = 0; 
            if( 0 != $nr++ ) return $sql;

            // Modified WHERE
            $sql['where'] = sprintf(
                " AND ( %s OR %s ) ",
                $wpdb->prepare( "{$wpdb->posts}.post_title like '%%%s%%'", $title),
                mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
            );
            return $sql;
        });
    }
});

I need to filter out the data Meta Values or Title, Any suggestions? Thanks in advance

2
  • did you intend to use = in the if statement and not == or ===? Have you confirmed there are indeed posts that meet this queries specifications?
    – Tom J Nowell
    Commented Feb 23, 2022 at 16:02
  • @TomJNowell if( $title = $q->get( '_meta_or_title' ) ) is equals to if($q->get( '_meta_or_title' ) ) $title = $q->get( '_meta_or_title' ); Commented Feb 23, 2022 at 16:15

1 Answer 1

1

From the documentation:

apply_filters_ref_array( 'get_meta_sql', string[] $sql, array $queries, string $type, string $primary_table, string $primary_id_column, object $context )

Filters the meta query’s generated SQL.

and

$type

(string) Type of meta. Possible values include but are not limited to 'post', 'comment', 'blog', 'term', and 'user'.

So because a hook like the one above can run multiple times on a page, which means your filter/closure could be called multiple times for the same posts query (i.e. WP_Query call), then you should do something like if ( 'post' === $type ) { modify the $sql['where'] } to modify the WHERE only if the meta type is exactly post.

And basically after I tested your code, I noticed that the conditional if( 0 != $nr++ ) return $sql; skipped modifying the post meta query and instead modified a term meta query, which means when the hook called your closure for the 1st time, it was for a term meta query.

Hence, that explains why the "whenever I use tax_query, it is not working".

How can you fix the issue

  1. Change the add_filter( 'get_meta_sql', function( $sql ) use ( $title ) to add_filter( 'get_meta_sql', function( $sql, $queries, $type ) use ( $title ).

  2. Change the return $sql; }); to return $sql; }, 10, 3); (i.e. let the above closure accepts 3 parameters).

  3. Then change the if( 0 != $nr++ ) return $sql; to if( 'post' !== $type || 0 != $nr++ ) return $sql;

Additional Code

You could use get_meta_sql alone without having to use pre_get_posts to add a filter for get_meta_sql, like so:

add_filter( 'get_meta_sql', 'my_get_meta_sql', 10, 6 );
function my_get_meta_sql( $sql, $queries, $type, $primary_table, $primary_id_column, $context ) {
    // Check that it's a post meta type and that the main query object is an instance of WP_Query.
    if ( 'post' === $type && $context instanceof WP_Query &&
        // And check that the _meta_or_title arg is set.
        ( $title = $context->get( '_meta_or_title' ) )
    ) {
        global $wpdb;

        $sql['where'] = 'YOUR CODE..';
    }

    return $sql;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.