WP_QUERY|如果在$args数组内|请帮助我仅在参数不为空时添加特定参数

时间:2020-02-24 作者:Jason Is My Name

我创建了一个相当复杂的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”关系非常有效。它只允许搜索标题、标题加上标记或类别、标记和类别,或单个标记或类别。但是,除非填充搜索词,否则它对关系“或”不起作用。。。

事实上,它实际上根本不承认“或”关系,它只对搜索词起作用。

1 个回复
最合适的回答,由SO网友:Antti Koskinen 整理而成

如果您还没有这样做,我建议您打开调试登录wp-config.php 因为这样可以更容易地跟踪代码中的错误。

// added to wp-config.php
define( \'WP_DEBUG\', true );
define( \'WP_DEBUG_LOG\', true ); // find error.log in /wp-content, shows backtrace for errors
define( \'WP_DEBUG_DISPLAY\', false ); // true if you want errors to be shown on screen
您可以做的第二件事是检查$_POST 带有三元运算符的参数($condition?true:false;)并设置任何所需的默认值。因为这可以防止任何无效的密钥错误,如果由于某种原因,您期望的密钥不在$_POST.

$paged              = ! empty( $_POST[\'page\'] ) ? $_POST[\'page\'] : \'\';
$display_count      = ! empty( $_POST[\'display_count\'] ) ? $_POST[\'display_count\'] : 5; // set default value as the "else" case
$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\'] : \'\';
以上empty() 检查密钥是否存在于$_POST 这是有价值的。如果是,则将该值设置为变量,否则该变量将获得一个空字符串或一些默认值作为其值。

就我个人而言,我不使用替代品if 语法,除非我将php添加到更长的html段中。这看起来有点干净,但这只是一个偏好的问题。

$offset_modifier = 0;
if ( \'prev\' === $direction ) {
  $offset_modifier = $paged - 2;
} else if ( \'prev\' === $direction ) {
  $offset_modifier = $paged;
} else if ( \'last\' === $direction ) {
  $offset_modifier = $paged - 1;
}
$offsetcalc = $offset_modifier * $display_count;
您可以设置默认值$args 在这些之前if 减少重复行数的语句。

$args = array(
  \'post_type\'         => \'product\',
  \'post_status\'       => \'publish\',
  \'orderby\'           => \'menu_order\',
  \'order\'             => \'ASC\',
  \'posts_per_page\'    => $display_count,
  \'page\'              => $paged, // maybe protect against negative numbers with ternary operator?
  \'offset\'            => $offsetcalc, // maybe protect against negative numbers with ternary operator?
  \'tax_query\'         => array(),
);
通过在代码开头设置搜索相关变量,您可以相信这些变量将始终被设置,并且它们要么为空,要么具有一些默认值,要么包含用户提交的值。您可以对变量进行简单检查,并将任何必需的参数添加到默认值中$args.

if ( $search_term ) {
  $args[\'s\'] = $search_term;
}

if ( $search_tags ) {
  $args[\'tax_query\'][\'product_tag\'] = array( // named keys are allowed, if I remember correctly
    \'taxonomy\'          => \'product_tag\',
    \'field\'             => \'slug\',
    \'terms\'             => array($search_tags),
  );
}

if ( $search_categories ) {
  $args[\'tax_query\'][\'product_cat\'] = array(
    \'taxonomy\'          => \'product_cat\',
    \'field\'             => \'slug\',
    \'terms\'             => array($search_categories),
  );
}

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;
}
如果需要,也可以稍后覆盖默认值。

if ( $some_condition ) {
  $args[\'post_type\'] = \'another_post_type\';  
}