将PRE_GET_POSTS用于ACF中继子字段上相似比较的元值

时间:2017-09-07 作者:Jordan Carter

我正在尝试根据repeater字段的ACF子字段值组织搜索结果页面。我想要一个相似的结果,而不是=。

到目前为止,我获得了以下信息(在下面的帮助下进行了修改,但仍然不起作用):

// Modify meta key to allow wildcard
function add_wildcard_to_meta_key_filter( $where ) {

  $where = str_replace("meta_key = \'test_repeater_%", "meta_key LIKE \'test_repeater_%", $where);

  return $where;
}
add_filter(\'posts_where\', \'add_wildcard_to_meta_key_filter\');

//Modify search query
function alter_search_query($query) {
  if ( !$query->is_search )
    return $query;

  $search = $query->query;

  $query->set(\'post_type\' ,\'page\');
  $query->set(\'meta_query\', array(
    array(
      \'meta_key\' => \'test_repeater_%_test_sub_field\',
      \'meta_value\' => \'%\'.$search.\'%\',
      \'compare\' => \'LIKE\',
    ),
  ));

  $query->set(\'s\', \'\');

}
add_action(\'pre_get_posts\',\'alter_search_query\');
如果我加上确切的值,即“beef tenderloin”,效果很好,但如果我输入“beef”,则失败。如何使搜索更加通用?

我安装了查询监视器插件,并注意到我得到了以下SQL

SELECT SQL_CALC_FOUND_ROWS wpfb_posts.ID
FROM wpfb_posts 
INNER JOIN wpfb_postmeta
ON ( wpfb_posts.ID = wpfb_postmeta.post_id )
WHERE 1=1 
AND ( ( wpfb_postmeta.meta_key LIKE \'test_repeater_%_test_sub_field\'
AND wpfb_postmeta.meta_value IN (\'beef\') ) )
AND wpfb_posts.post_type = \'page\'
AND (wpfb_posts.post_status = \'publish\'
OR wpfb_posts.post_status = \'acf-disabled\'
OR wpfb_posts.post_status = \'private\')
GROUP BY wpfb_posts.ID
ORDER BY wpfb_posts.post_date DESC
LIMIT 0, 10
如果我更改此选项:

AND ( ( wpfb_postmeta.meta_key LIKE \'test_repeater_%_test_sub_field\'
AND wpfb_postmeta.meta_value IN (\'beef\') ) )
对此:

AND ( ( wpfb_postmeta.meta_key LIKE \'test_repeater_%_test_sub_field\'
AND wpfb_postmeta.meta_value LIKE \'%beef%\' ) )
然后在phpMyAdmin的SQL中运行它,我得到了所需的行。问题是,如何使用WordPress函数更改查询的外观?

2 个回复
SO网友:HU ist Sebastian

您需要在meta\\u值中添加通配符。

改变

$query->set(\'meta_query\', array(
    array(
      \'meta_key\' => \'test_repeater_%_test_sub_field\',
      \'meta_value\' => $search,
      \'compare\' => \'LIKE\',
    ),
  ));

$query->set(\'meta_query\', array(
    array(
      \'meta_key\' => \'test_repeater_%_test_sub_field\',
      \'meta_value\' => \'%\'.$search.\'%\',
      \'compare\' => \'LIKE\',
    ),
  ));

SO网友:Muhammad Asad

首先,您需要循环中继器有多少次值示例

anada\\u number\\u 0\\u anada\\u number
anada\\u number\\u 1\\u anada\\u number
anada\\u number\\u 2\\u anada\\u number

在我的情况下,我有五次转发器字段

for( $i=0; $i<5; $i++ ){ 
                    $meta_query[\'anada_number\'][] = array(
                        \'key\' => \'anada_number_\'.$i.\'_anada_number\',
                        \'value\' => $anada_number,
                    );
                }
然后在查询参数中使用它

$arg = array(
$meta_query[\'anada_number\']
);

$wp_query = new WP_Query($arg); 

结束

相关推荐

将PRE_GET_POSTS用于ACF中继子字段上相似比较的元值 - 小码农CODE - 行之有效找到问题解决它

将PRE_GET_POSTS用于ACF中继子字段上相似比较的元值

时间:2017-09-07 作者:Jordan Carter

我正在尝试根据repeater字段的ACF子字段值组织搜索结果页面。我想要一个相似的结果,而不是=。

到目前为止,我获得了以下信息(在下面的帮助下进行了修改,但仍然不起作用):

// Modify meta key to allow wildcard
function add_wildcard_to_meta_key_filter( $where ) {

  $where = str_replace("meta_key = \'test_repeater_%", "meta_key LIKE \'test_repeater_%", $where);

  return $where;
}
add_filter(\'posts_where\', \'add_wildcard_to_meta_key_filter\');

//Modify search query
function alter_search_query($query) {
  if ( !$query->is_search )
    return $query;

  $search = $query->query;

  $query->set(\'post_type\' ,\'page\');
  $query->set(\'meta_query\', array(
    array(
      \'meta_key\' => \'test_repeater_%_test_sub_field\',
      \'meta_value\' => \'%\'.$search.\'%\',
      \'compare\' => \'LIKE\',
    ),
  ));

  $query->set(\'s\', \'\');

}
add_action(\'pre_get_posts\',\'alter_search_query\');
如果我加上确切的值,即“beef tenderloin”,效果很好,但如果我输入“beef”,则失败。如何使搜索更加通用?

我安装了查询监视器插件,并注意到我得到了以下SQL

SELECT SQL_CALC_FOUND_ROWS wpfb_posts.ID
FROM wpfb_posts 
INNER JOIN wpfb_postmeta
ON ( wpfb_posts.ID = wpfb_postmeta.post_id )
WHERE 1=1 
AND ( ( wpfb_postmeta.meta_key LIKE \'test_repeater_%_test_sub_field\'
AND wpfb_postmeta.meta_value IN (\'beef\') ) )
AND wpfb_posts.post_type = \'page\'
AND (wpfb_posts.post_status = \'publish\'
OR wpfb_posts.post_status = \'acf-disabled\'
OR wpfb_posts.post_status = \'private\')
GROUP BY wpfb_posts.ID
ORDER BY wpfb_posts.post_date DESC
LIMIT 0, 10
如果我更改此选项:

AND ( ( wpfb_postmeta.meta_key LIKE \'test_repeater_%_test_sub_field\'
AND wpfb_postmeta.meta_value IN (\'beef\') ) )
对此:

AND ( ( wpfb_postmeta.meta_key LIKE \'test_repeater_%_test_sub_field\'
AND wpfb_postmeta.meta_value LIKE \'%beef%\' ) )
然后在phpMyAdmin的SQL中运行它,我得到了所需的行。问题是,如何使用WordPress函数更改查询的外观?

2 个回复
SO网友:HU ist Sebastian

您需要在meta\\u值中添加通配符。

改变

$query->set(\'meta_query\', array(
    array(
      \'meta_key\' => \'test_repeater_%_test_sub_field\',
      \'meta_value\' => $search,
      \'compare\' => \'LIKE\',
    ),
  ));

$query->set(\'meta_query\', array(
    array(
      \'meta_key\' => \'test_repeater_%_test_sub_field\',
      \'meta_value\' => \'%\'.$search.\'%\',
      \'compare\' => \'LIKE\',
    ),
  ));

SO网友:Muhammad Asad

首先,您需要循环中继器有多少次值示例

anada\\u number\\u 0\\u anada\\u number
anada\\u number\\u 1\\u anada\\u number
anada\\u number\\u 2\\u anada\\u number

在我的情况下,我有五次转发器字段

for( $i=0; $i<5; $i++ ){ 
                    $meta_query[\'anada_number\'][] = array(
                        \'key\' => \'anada_number_\'.$i.\'_anada_number\',
                        \'value\' => $anada_number,
                    );
                }
然后在查询参数中使用它

$arg = array(
$meta_query[\'anada_number\']
);

$wp_query = new WP_Query($arg); 

相关推荐