我创建了一个相当复杂的WP\\u查询,我需要根据用户的输入限制查询中使用的参数。
我很感激我可以将整个参数包装在IF语句中,但是如果我尝试在参数中添加IF语句,它会破坏网站,声称代码中存在严重错误。
由于我在将这些变量添加到参数列表之前要检查多个变量,因此我希望可以使用以下代码:
$paged = $_POST[\'page\'];
$display_count = $_POST[\'display_count\'];
$direction = $_POST[\'direction\'];
$search_term = $_POST[\'search_term\'];
$search_tags = $_POST[\'search_tags\'];
$search_categories = $_POST[\'search_categories\'];
$search_relation = $_POST[\'search_relation\'];
if ($direction == \'prev\') :
$offsetcalc = (( $paged - 2 ) * $display_count);
elseif ($direction == \'next\') :
$offsetcalc = ($paged * $display_count);
elseif ($direction == \'last\') :
$offsetcalc = (( $paged - 1 ) * $display_count);
else :
$offsetcalc = 0;
endif;
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
if ($search_term != \'\') :
\'s\' => $search_term,
endif;
\'tax_query\' => array(
\'relation\' => $search_relation,
if ($search_tags != \'\') :
array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => array($search_tags),
\'operator\' => $search_relation
),
endif;
if ($search_categories != \'\') :
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => array($search_categories),
\'operator\' => $search_relation
)
endif;
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
但是,这会返回以下错误:
There has been a critical error on your website.
您可以确保所使用的变量都是正确的,这是可以工作的代码,没有在$args中使用if语句。
我还尝试在args数组之外创建变量,如下所示:
$paged = $_POST[\'page\'];
$display_count = $_POST[\'display_count\'];
$direction = $_POST[\'direction\'];
$search_term = $_POST[\'search_term\'];
$search_tags = $_POST[\'search_tags\'];
$search_categories = $_POST[\'search_categories\'];
$search_relation = $_POST[\'search_relation\'];
if ($search_term != \'\') :
$search_term_not_blank = "\'s\' => $search_term,";
endif;
if ($direction == \'prev\') :
$offsetcalc = (( $paged - 2 ) * $display_count);
elseif ($direction == \'next\') :
$offsetcalc = ($paged * $display_count);
elseif ($direction == \'last\') :
$offsetcalc = (( $paged - 1 ) * $display_count);
else :
$offsetcalc = 0;
endif;
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
echo $search_term_not_blank;
\'tax_query\' => array(
\'relation\' => $search_relation,
array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => array($search_tags),
\'operator\' => $search_relation
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => array($search_categories),
\'operator\' => $search_relation
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
再次产生相同的错误:
There has been a critical error on your website.
我相信,如果if语句环绕整个args数组,它确实会起作用,但是,当我检查多个变量时,在如此大的wp\\u查询中使用if、elseif、elseif、else是相当庞大的。我需要的是:
if (searchterm != \'\' & tags != \'\' & categories != \'\')
elseif (searchterm == \'\' & tags != \'\' & categories != \'\')
elseif (searchterm != \'\' & tags == \'\' & categories != \'\')
elseif (searchterm != \'\' & tags != \'\' & categories == \'\')
elseif (searchterm == \'\' & tags == \'\' & categories != \'\')
elseif (searchterm != \'\' & tags == \'\' & categories == \'\')
elseif (searchterm == \'\' & tags != \'\' & categories == \'\')
else (searchterm == \'\' & tags == \'\' & categories == \'\')
EDIT (我现在也尝试了long方法,if、elseif、else):
$paged = $_POST[\'page\'];
$display_count = $_POST[\'display_count\'];
$direction = $_POST[\'direction\'];
$search_term = $_POST[\'search_term\'];
$search_tags = $_POST[\'search_tags\'];
$search_categories = $_POST[\'search_categories\'];
$search_relation = $_POST[\'search_relation\'];
if ($direction == \'prev\') :
$offsetcalc = (( $paged - 2 ) * $display_count);
elseif ($direction == \'next\') :
$offsetcalc = ($paged * $display_count);
elseif ($direction == \'last\') :
$offsetcalc = (( $paged - 1 ) * $display_count);
else :
$offsetcalc = 0;
endif;
if (isset($search_term) && isset($search_tags) && isset($search_categories)) :
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
\'s\' => $search_term,
\'tax_query\' => array(
\'relation\' => $search_relation,
array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => array($search_tags),
\'operator\' => $search_relation
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => array($search_categories),
\'operator\' => $search_relation
)
)
);
elseif (empty($search_term) && isset($search_tags) && isset($search_categories)) :
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
\'tax_query\' => array(
\'relation\' => $search_relation,
array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => array($search_tags),
\'operator\' => $search_relation
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => array($search_categories),
\'operator\' => $search_relation
)
)
);
elseif (isset($search_term) && empty($search_tags) && isset($search_categories)) :
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
\'s\' => $search_term,
\'tax_query\' => array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => array($search_categories),
\'operator\' => $search_relation
)
);
elseif (isset($search_term) && isset($search_tags) && empty($search_categories)) :
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
\'s\' => $search_term,
\'tax_query\' => array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => array($search_tags),
\'operator\' => $search_relation
)
);
elseif (empty($search_term) && empty($search_tags) && isset($search_categories)) :
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
\'tax_query\' => array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => array($search_categories),
\'operator\' => $search_relation
)
);
elseif (isset($search_term) && empty($search_tags) && empty($search_categories)) :
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
\'s\' => $search_term
);
elseif (empty($search_term) && empty($search_tags) && empty($search_categories)) :
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc
);
endif;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
此方法有效,不包括选择关系“或”时,它始终按原样返回所有产品,继续搜索空白搜索项。这有什么解决办法吗?如果为空,则被告知不包括args[\'s\']
最后,我在WordPress的功能范围内工作。php文件。这是我在搜索时调用的函数。
当然有办法做到这一点吗?
非常感谢贡献者Jason。
EDIT (回应安蒂的回答):
我已经尝试在适用的地方插入您的代码,这是它的荣耀:
$paged = ! empty( $_POST[\'page\'] ) ? $_POST[\'page\'] : 1;
$display_count = ! empty( $_POST[\'display_count\'] ) ? $_POST[\'display_count\'] : 9;
$direction = ! empty( $_POST[\'direction\'] ) ? $_POST[\'direction\'] : \'\';
$search_term = ! empty( $_POST[\'search_term\'] ) ? $_POST[\'search_term\'] : \'\';
$search_tags = ! empty( $_POST[\'search_tags\'] ) ? $_POST[\'search_tags\'] : \'\';
$search_categories = ! empty( $_POST[\'search_categories\'] ) ? $_POST[\'search_categories\'] : \'\';
$search_relation = ! empty( $_POST[\'search_relation\'] ) ? $_POST[\'search_relation\'] : \'AND\';
$offset_modifier = 0;
if ( $direction === \'prev\' ) :
$offset_modifier = $paged - 2;
elseif ( $direction === \'next\' ) :
$offset_modifier = $paged;
elseif ( $direction === \'last\' ) :
$offset_modifier = $paged - 1;
endif;
$offsetcalc = $offset_modifier * $display_count;
if ( $search_term ) {
$args[\'s\'] = $search_term;
}
if ( $search_tags ) {
$args[\'tax_query\'][\'product_tag\'] = array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => array($search_tags),
\'operator\' => $search_relation
);
}
if ( $search_categories ) {
$args[\'tax_query\'][\'product_cat\'] = array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => array($search_categories),
\'operator\' => $search_relation
);
}
if ( $search_tags && $search_categories ) {
$args[\'tax_query\'][\'relation\'] = $search_relation;
$args[\'tax_query\'][\'product_tag\'][\'relation\'] = $search_relation;
$args[\'tax_query\'][\'product_cat\'][\'relation\'] = $search_relation;
}
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
\'tax_query\' => array(),
);
这会很好地加载页面,但是如果选择了任何不同的过滤器,则搜索结果不会改变。
EDIT (对Antti第2部分的回应):
$paged = ! empty( $_POST[\'page\'] ) ? $_POST[\'page\'] : 1;
$display_count = ! empty( $_POST[\'display_count\'] ) ? $_POST[\'display_count\'] : 9;
$direction = ! empty( $_POST[\'direction\'] ) ? $_POST[\'direction\'] : \'\';
$search_term = ! empty( $_POST[\'search_term\'] ) ? $_POST[\'search_term\'] : \'\';
$search_tags = ! empty( $_POST[\'search_tags\'] ) ? $_POST[\'search_tags\'] : \'\';
$search_categories = ! empty( $_POST[\'search_categories\'] ) ? $_POST[\'search_categories\'] : \'\';
$search_relation = ! empty( $_POST[\'search_relation\'] ) ? $_POST[\'search_relation\'] : \'AND\';
$offset_modifier = 0;
if ( $direction === \'prev\' ) :
$offset_modifier = $paged - 2;
elseif ( $direction === \'next\' ) :
$offset_modifier = $paged;
elseif ( $direction === \'last\' ) :
$offset_modifier = $paged - 1;
endif;
$offsetcalc = $offset_modifier * $display_count;
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => $display_count,
\'page\' => $paged,
\'offset\' => $offsetcalc,
\'tax_query\' => array()
);
if ( isset($search_term) ) :
$args[\'s\'] = $search_term;
endif;
if ( $search_tags ) :
$args[\'tax_query\'][\'product_tag\'] = array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => array($search_tags),
\'operator\' => $search_relation
);
endif;
if ( $search_categories ) :
$args[\'tax_query\'][\'product_cat\'] = array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => array($search_categories),
\'operator\' => $search_relation
);
endif;
if ( $search_tags && $search_categories ) :
$args[\'tax_query\'][\'relation\'] = $search_relation;
$args[\'tax_query\'][\'product_tag\'][\'relation\'] = $search_relation;
$args[\'tax_query\'][\'product_cat\'][\'relation\'] = $search_relation;
endif;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
现在,这对于“AND”关系非常有效。它只允许搜索标题、标题加上标记或类别、标记和类别,或单个标记或类别。但是,除非填充搜索词,否则它对关系“或”不起作用。。。
事实上,它实际上根本不承认“或”关系,它只对搜索词起作用。