参数[‘s’]和参数[‘META_QUERY’]之间具有OR关系的WP_QUERY

时间:2017-07-06 作者:s.k.

我需要在args[\'s\']和args[\'meta\\u query\']之间创建“OR”关系。当我定义

“relation”=>“OR”在数组列表中,它仍然生成与和相关的sql。

下面是代码段

   if( isset($_GET[\'resume_search_keyword\']) && $_GET[\'resume_search_keyword\'] != \'\' ) :
    $resume_args[\'s\'] = $_GET[\'resume_search_keyword\'];
   endif;

    $resume_search_skills = array(
      \'key\' => \'_resume_skills\',
      \'value\' => $_GET[\'resume_search_keyword\'],
      \'compare\' => \'LIKE\'
    );

    $resume_search_job_duties = array(
      \'key\' => \'_resume_job_duties\',
      \'value\' => $_GET[\'resume_search_keyword\'],
      \'compare\' => \'LIKE\'
    );

  $resume_args[\'meta_query\'] = array(
    \'relation\' => "OR",
    $resume_search_job_duties,
    $resume_search_skills
  );

  $resumes = new WP_Query($resume_args);
为其生成的sql如下所示

SELECT SQL_CALC_FOUND_ROWS
    wp_posts.ID
FROM
    wp_posts
        INNER JOIN
    wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE
    1 = 1
        AND (((wp_posts.post_title LIKE \'%teaching%\')
        OR (wp_posts.post_excerpt LIKE \'%teaching%\')
        OR (wp_posts.post_content LIKE \'%teaching%\')))
        AND ((wp_postmeta.meta_key = \'_resume_job_duties\'
        AND wp_postmeta.meta_value LIKE \'%teaching%\')
        OR (wp_postmeta.meta_key = \'_resume_skills\'
        AND wp_postmeta.meta_value LIKE \'%teaching%\'))
        AND wp_posts.post_type = \'resume\'
        AND (wp_posts.post_status = \'publish\'
        OR wp_posts.post_author = 3
        AND wp_posts.post_status = \'private\')
我要找的是

SELECT SQL_CALC_FOUND_ROWS
    wp_posts.ID
FROM
    wp_posts
        INNER JOIN
    wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE
    1 = 1
        AND (((wp_posts.post_title LIKE \'%teaching%\')
        OR (wp_posts.post_excerpt LIKE \'%teaching%\')
        OR (wp_posts.post_content LIKE \'%teaching%\')))
        **OR** ((wp_postmeta.meta_key = \'_resume_job_duties\'
        AND wp_postmeta.meta_value LIKE \'%teaching%\')
        OR (wp_postmeta.meta_key = \'_resume_skills\'
        AND wp_postmeta.meta_value LIKE \'%teaching%\'))
        AND wp_posts.post_type = \'resume\'
        AND (wp_posts.post_status = \'publish\'
        OR wp_posts.post_author = 3
        AND wp_posts.post_status = \'private\')
有人能帮我一下吗?我哪里做错了?

1 个回复
SO网友:berend

Try this:

$query = new WP_Query( array(
    \'meta_query\' => array(
        \'relation\' => \'OR\',
        \'resume_skills_clause\' => array(
            \'key\' => \'_resume_skills\',
            \'value\' => $_GET[\'resume_search_keyword\'],
            \'compare\' => \'LIKE\'
        ),
        \'resume_job_duties_clause\' => array(
            \'key\' => "_resume_job_duties",
            \'value\' => $_GET[\'resume_search_keyword\'],
            \'compare\' => \'LIKE\'
        )
    )
));
结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post