admin管理员组文章数量:1194736
This is my first attempt at modifying the main query using the pre_get_posts
action/filter
This is the function currently hooked to it:
function advanced_product_search_filter($query)
{
if(!is_admin() &&
is_main_query() &&
is_search() && $query->query_vars['post_type'] == 'product')
{
$keyword = $_GET['s'];
if($_GET['exactly'])
$keyword = $_GET['s'] . ' "'. $_GET['exactly'] . '"';
if($_GET['without'])
{
$excluded = exclude_product_keyword_search($_GET['without'],
$_GET['pname'],
$_GET['pcode']
);
$query->set('post__not_in', $excluded);
}
if($_GET['pname'])
$query->set('s', $keyword);
else
$query->set('s', '');
if($_GET['pcode'])
{
$tax_queries = $query->get('tax_query');
$tax_queries[] = array(
array(
'taxonomy' => 'pa_ordering-code',
'field' => 'name',
'terms' => array($keyword),
'operator' => 'LIKE'
)
);
$query->set('tax_query', $tax_queries);
}
}
}
This is a plugin built on top of a WooCommerce
installation. So basically this function tries to support searching a WooCommerce
product either through the standard search (title and content) or an attribute search on a custom attribute called Ordering Code
which is pa_ordering-code
in the term_taxonomy
table
Other parts of the code works fine, I can modify the s
and post__not_in
vars without any problems, but if I try to use the tax_query
it, the query seems to break down.
To give you an idea, when I try this:
?s=foo&post_type=product&pname=1&pcode=1&without=non&exactly=foo+bar
dumping the WHERE
clause, I see this:
AND wp_posts.ID NOT IN (219)
AND 0 = 1
AND
(
(
(wp_posts.post_title LIKE '%foo%') OR
(wp_posts.post_content LIKE '%foo%')
) AND (
(wp_posts.post_title LIKE '%foo bar%') OR
(wp_posts.post_content LIKE '%foo bar%')
)
)
AND wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
AND
(
(
wp_postmeta.meta_key = '_visibility' AND
CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','search')
)
)
Notice the AND 0 = 1
? When I dump the JOIN
clause, I become sure that the tax_query
parameter isn't being interpreted properly since I only see the postmeta
table in there. No terms
, term_relationships
, or term_taxonomy
table.
And I also tried simply overwriting the existing tax_query
(if there is one) by doing this:
$args = array(
array(
'taxonomy' => 'pa_ordering-code',
'field' => 'name',
'terms' => array($keyword),
'operator' => 'LIKE'
)
);
$query->set('tax_query', $args);
But I still could not get it to work. Any ideas as to what I may be doing wrong?
This is my first attempt at modifying the main query using the pre_get_posts
action/filter
This is the function currently hooked to it:
function advanced_product_search_filter($query)
{
if(!is_admin() &&
is_main_query() &&
is_search() && $query->query_vars['post_type'] == 'product')
{
$keyword = $_GET['s'];
if($_GET['exactly'])
$keyword = $_GET['s'] . ' "'. $_GET['exactly'] . '"';
if($_GET['without'])
{
$excluded = exclude_product_keyword_search($_GET['without'],
$_GET['pname'],
$_GET['pcode']
);
$query->set('post__not_in', $excluded);
}
if($_GET['pname'])
$query->set('s', $keyword);
else
$query->set('s', '');
if($_GET['pcode'])
{
$tax_queries = $query->get('tax_query');
$tax_queries[] = array(
array(
'taxonomy' => 'pa_ordering-code',
'field' => 'name',
'terms' => array($keyword),
'operator' => 'LIKE'
)
);
$query->set('tax_query', $tax_queries);
}
}
}
This is a plugin built on top of a WooCommerce
installation. So basically this function tries to support searching a WooCommerce
product either through the standard search (title and content) or an attribute search on a custom attribute called Ordering Code
which is pa_ordering-code
in the term_taxonomy
table
Other parts of the code works fine, I can modify the s
and post__not_in
vars without any problems, but if I try to use the tax_query
it, the query seems to break down.
To give you an idea, when I try this:
?s=foo&post_type=product&pname=1&pcode=1&without=non&exactly=foo+bar
dumping the WHERE
clause, I see this:
AND wp_posts.ID NOT IN (219)
AND 0 = 1
AND
(
(
(wp_posts.post_title LIKE '%foo%') OR
(wp_posts.post_content LIKE '%foo%')
) AND (
(wp_posts.post_title LIKE '%foo bar%') OR
(wp_posts.post_content LIKE '%foo bar%')
)
)
AND wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
AND
(
(
wp_postmeta.meta_key = '_visibility' AND
CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','search')
)
)
Notice the AND 0 = 1
? When I dump the JOIN
clause, I become sure that the tax_query
parameter isn't being interpreted properly since I only see the postmeta
table in there. No terms
, term_relationships
, or term_taxonomy
table.
And I also tried simply overwriting the existing tax_query
(if there is one) by doing this:
$args = array(
array(
'taxonomy' => 'pa_ordering-code',
'field' => 'name',
'terms' => array($keyword),
'operator' => 'LIKE'
)
);
$query->set('tax_query', $args);
But I still could not get it to work. Any ideas as to what I may be doing wrong?
Share Improve this question asked Jun 21, 2012 at 10:23 Rolando CruzRolando Cruz 1391 silver badge2 bronze badges3 Answers
Reset to default 6The 0 = 1
happens when the terms don't exist.
You're creating a query for some set of terms. Before it can add the SQL to that main query, it first does a secondary query to get the term_id's for the relevant terms. If none are found, then it returns 0 = 1 instead, to shortcut the main query because no terms match the request.
Check definition of your "pa_ordering-code" taxonomy.
It looks like you could have wrong value in "query_var" attribute.
This code could casue similar efect:
register_taxonomy(...
'query_var' => true,
));
If so, remove (or change) query_var part and check again.
In your tax_query
, try setting field
to term_taxonomy_id
and using $term->term_taxonomy_id
instead of $term->term_id
:
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'tax_query' => [
[
'taxonomy' => 'my_custom_taxonomy',
'field' => 'term_taxonomy_id',
'terms' => $term->term_taxonomy_id,
]
]
];
$query = new WP_Query($args);
本文标签: custom taxonomyUsing taxquery creates a 10 or 11 in wpquerygtrequest
版权声明:本文标题:custom taxonomy - Using tax_query creates a 1 = 0 or 1 = 1 in $wp_query->request 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738430479a2086370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论