admin管理员组文章数量:1122832
I have a need to create "OR" relation between args['s'] and args['meta_query']. When I define
'relation' => "OR" within the list of arrays it still generates the sql with AND relation.
Code snippet is following
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);
The sql generated for the same is as follows
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')
What I am looking for instead is
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')
Can someone please help where am I going wrong?
I have a need to create "OR" relation between args['s'] and args['meta_query']. When I define
'relation' => "OR" within the list of arrays it still generates the sql with AND relation.
Code snippet is following
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);
The sql generated for the same is as follows
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')
What I am looking for instead is
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')
Can someone please help where am I going wrong?
Share Improve this question asked Jul 6, 2017 at 11:21 s.k.s.k. 112 bronze badges 01 Answer
Reset to default 0Try 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 queryWPquery with OR relation between args39s39 and arg39metaquery39
版权声明:本文标题:wp query - WP_query with OR relation between args['s'] and arg['meta_query'] 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736292806a1929013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论