Search 扩展wordpress搜索查询

Search 扩展wordpress搜索查询,search,wordpress,Search,Wordpress,我有一个工作搜索查询,每个页面有偏移量和post_(用户可以浏览页面) 它工作得很好。 但是现在我需要在搜索函数中添加一个元字段。它应该在s=>$query或metafield=>$query 大概是这样的: $args = array( 'post_type' => 'type', 'posts_per_page' => $the_count, 'offset' => ($the_count*$the_c_page )-$the_count, 'meta_q

我有一个工作搜索查询,每个页面有偏移量和post_(用户可以浏览页面)

它工作得很好。 但是现在我需要在搜索函数中添加一个元字段。它应该在
s=>$query
metafield=>$query
大概是这样的:

$args = array(
  'post_type' => 'type',
  'posts_per_page' => $the_count,
  'offset' =>  ($the_count*$the_c_page )-$the_count,
  'meta_query' => array(
    'relation' => 'OR',
    's'=>$the_str,
    array(
      'key' => 'key',
      'value' => $the_str
    )
);
$the_query = new WP_Query( $args );

不幸的是,这将只在第二个条件之后搜索。有人有什么想法吗?

如果你打开
query.php
你会发现它实际上并没有考虑到这一点。这并不是说这不是一个好主意,而是使用
将每个单独的查询部分连接到主
,其中
。因此,“title包含XYZ”
“无论元查询是什么”
“无论税务查询是什么”,等等。
都有子逻辑,支持
以及
,但在这些外部部分之间无法执行此操作

但是,您可以通过点击其中一个过滤器来完成所需的操作,可能是
get\u meta\u sql
。元查询返回一条语句,该语句将放入您希望成为
,因此您可以查找并替换它:

function change_and_to_or_for_meta( $meta_query, $type, $primary_table, $primary_id_column, $context )
{
   if( 0 === strpos( $meta_query['where'], ' AND' ) )
   {
      $meta_query['where'] = ' OR' . substr( $meta_query['where'], 4 );
   }

   return $meta_query;
}

//Add our filter to replace the AND at the start with OR
add_filter( 'get_meta_sql', 'change_and_to_or_for_meta', 10, 5 );
$query = new WP_Query( $args );
//Remove our filter so we don't mess other things up
remove_filter( 'get_meta_sql', 'change_and_to_or_for_meta', 10 );

我认为您无法让
WP\u Query
在主搜索和元搜索之间执行
。另一种选择是合并它们,但这会影响到你需要手动处理的每页
帖子。我知道。我已经通过两个问题找到了解决方案。但就像你sead一样,它破坏了我的页面选择。wordpress必须有一个更好的解决方案……这太棒了!很好!非常感谢。
function change_and_to_or_for_meta( $meta_query, $type, $primary_table, $primary_id_column, $context )
{
   if( 0 === strpos( $meta_query['where'], ' AND' ) )
   {
      $meta_query['where'] = ' OR' . substr( $meta_query['where'], 4 );
   }

   return $meta_query;
}

//Add our filter to replace the AND at the start with OR
add_filter( 'get_meta_sql', 'change_and_to_or_for_meta', 10, 5 );
$query = new WP_Query( $args );
//Remove our filter so we don't mess other things up
remove_filter( 'get_meta_sql', 'change_and_to_or_for_meta', 10 );