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
=
in the if statement and not==
or===
? Have you confirmed there are indeed posts that meet this queries specifications?if( $title = $q->get( '_meta_or_title' ) )
is equals toif($q->get( '_meta_or_title' ) ) $title = $q->get( '_meta_or_title' );