我是WP的新手,我还没有对我提出的这种方法进行过太多测试。也许你可以帮我检查一下我是否正确。到目前为止,我找到的解决方案是实现相同的meta\\u查询逻辑,只是进行一些替换。
首先,用法:
$args = array(
\'lang\' => \'pt\', //this function does not conflict (example: polylang)
\'post_type\' => \'produtos\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 10,
\'paged\' => 1,
\'fields\' => \'ids\',
);
$args[\'meta_query] = [
[\'relation\'] => \'OR\'; //any relation you desire
[
\'key\' => \'acf_field\', //any custom field (regular usage)
\'value\' => $search, //any value (regular usage)
\'compare\' => \'LIKE\', //any comparition (regular usage)
],
[
\'key\' => \'post_title\', //set the default post content of wordpress you desire (\'post_content\', \'post_title\' and \'post_excerpt\')
\'value\' => $search, //any value
\'compare\' => \'LIKE\', //tested with \'LIKE and \'=\', works great and I can\'t realize other needs.
],
[
\'key\' => \'post_exerpt\', // you can add how many times you need
\'value\' => $search_2,
\'compare\' => \'LIKE\',
],
];
$the_query = new WP_Query( $args ); //jus query
wp_reset_postdata(); //clean your query
若要工作,请将此函数添加到主题函数中。php
function post_content_to_meta_queries($where, $wp_query){
global $wpdb;
//if there is no metaquery, bye!
$meta_queries = $wp_query->get( \'meta_query\' );
if( !$meta_queries || $meta_queries == \'\' ) return $where;
//if only one relation
$where = str_replace($wpdb->postmeta . ".meta_key = \'post_title\' AND " . $wpdb->postmeta . ".meta_value", $wpdb->posts . ".post_title", $where);
$where = str_replace($wpdb->postmeta . ".meta_key = \'post_content\' AND " . $wpdb->postmeta . ".meta_value", $wpdb->posts . ".post_content", $where);
$where = str_replace($wpdb->postmeta . ".meta_key = \'post_excerpt\' AND " . $wpdb->postmeta . ".meta_value", $wpdb->posts . ".post_excerpt", $where);
////for nested relations
//count the numbers of meta queries for possible replacements
$number_of_relations = count($meta_queries);
//replace \'WHERE\' using the multidimensional postmeta naming logic used by wordpress core
$i = 1;
while($i<=$number_of_relations && $number_of_relations > 0){
$where = str_replace("mt".$i.".meta_key = \'post_title\' AND mt".$i.".meta_value", $wpdb->posts . ".post_title", $where);
$where = str_replace("mt".$i.".meta_key = \'post_content\' AND mt".$i.".meta_value", $wpdb->posts . ".post_content", $where);
$where = str_replace("mt".$i.".meta_key = \'post_excerpt\' AND mt".$i.".meta_value", $wpdb->posts . ".post_excerpt", $where);
$i++;
}
return $where;
}
add_filter(\'posts_where\',\'post_content_to_meta_queries\',10,2);
我很肯定这是可以改进的!希望有帮助!