As suggested in WP Query Args - Title or Meta Value I tried to query my Custom Content Type on Title and Meta Value alternatively.
It almost works, but only if I do not introduce a tax query (and I need it!!)
// general args
$args = array(
'post_type' => 'linea',
'posts_per_page' => 10,
);
// tax args
$args['tax_query'] = array( 'relation' => 'AND',
array(
'taxonomy' => 'stagione_linea',
'field' => 'slug',
'terms' => 'summer'
) );
// meta args
$args['meta_query'] = array(
array(
'key' => 'codice_linea',
'value'=> $term,
'compare' => 'LIKE',
)
);
// special arg here
$args['_meta_or_title'] = $term;
Filter function is the one I copied from the article above. I tried to echo out something to understand the problem.
add_action( 'pre_get_posts', 'searchByTitleOrMeta');
function searchByTitleOrMeta( $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;
var_dump($sql['where'); die(); // DEBUG HERE!
$sql['where'] = sprintf(
" AND ( %s OR %s ) ",
$wpdb->prepare( "{$wpdb->posts}.post_title like '%s' ", "%".$wpdb->esc_like($title)."%"),
mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
);
return $sql;
});
}
};
With no tax query (suppose $term = 'lorem')
["where"]=>
string(395) " AND ( tm_posts.post_title like '{0acc5f40973efce47f8c42124c3734c97cc594f755a60224e0dd4cb02827c6b6}lorem{0acc5f40973efce47f8c42124c3734c97cc594f755a60224e0dd4cb02827c6b6}' OR (
( tm_postmeta.meta_key = 'codice_linea' AND tm_postmeta.meta_value LIKE '{0acc5f40973efce47f8c42124c3734c97cc594f755a60224e0dd4cb02827c6b6}lorem{0acc5f40973efce47f8c42124c3734c97cc594f755a60224e0dd4cb02827c6b6}' )
If I set the tax query
["where"]=>
string(177) " AND ( tm_posts.post_title like '{0406194437bac6b8187ce05f3bb4eecb83f0178715b7c8ca750a672ba53df8ac}lorem{0406194437bac6b8187ce05f3bb4eecb83f0178715b7c8ca750a672ba53df8ac}' OR ) "
I don't understand why...