WP_QUERY如果满足变量,则使用ARRAY_PUSH添加参数

时间:2017-07-23 作者:Jeronimo

如果变量是,我需要在wp\\u查询中添加更多参数!通过快捷码时为空。我正在传递循环中要使用的几个自定义字段名的名称。

我希望它像这样工作:如果custom\\u field\\u 1是!空,但custom\\u field\\u 2为空,请将第一个数组添加到$args数组。如果custom\\u field\\u 1为!空且custom\\u field\\u 2为!空,将第二个数组添加到$args数组。

我觉得我在大致范围内,但它工作不正常。如果我手动将每个meta\\u查询添加到$args数组,它将返回正确的数据。

任何帮助都将不胜感激。

// Attributes
extract( shortcode_atts(
    array(
        \'pagination\' => \'true\',
        \'query\' => \'\',
        \'category\' => \'\',
        \'tag_name\' => \'\',
        \'custom_field_1\' => \'\',
        \'custom_field_2\' => \'\',
        \'relation_operator\' => \'\',
        \'number_posts\' => \'\',
        \'order_by\'   => \'\',
        \'order\'     => \'\',
    ), $atts ));

$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;

$custom_field_query = array ();

// if $custom_field_1 is not empty and $custom_field_2 is empty 
if (!empty($custom_field_1) && empty($custom_field_2) ) {
    array_push($custom_field_query, array (
        \'meta_query\' => array(
            array(
                \'key\'     => $custom_field_1,
                \'value\'   => \'\',
                \'compare\' => \'!=\'
            ) ) ) );
} 

// if $custom_field_1 is not empty and $custom_field_2 is not empty
if (!empty($custom_field_1) && !empty($custom_field_2) ) {
    array_push($custom_field_query, array (
        \'meta_query\' => array(
            \'relation\' => $relation_operator,
            array(
                \'key\'     => $custom_field_1,
                \'value\'   => \'\',
                \'compare\' => \'!=\',
            ),
            array(
                \'key\'     => $custom_field_2,
                \'value\'   => \'\',
                \'compare\' => \'!=\',
            ) ) ) );    
} 

// if $custom_field_query has array custom_field_query gets added to $args
if(!empty($custom_field_query)){
    $args[\'custom_field_query\'] = $custom_field_query;
}   

// WP_Query arguments
$args = array(
    \'paged\'                 => $paged,
    \'post_type\'             => array( \'page\', \' post\' ),
    \'post_status\'           => array( \'publish\' ),
    \'category_name\'         => $category,
    \'tag\'                   => $tag_name,
    \'category__in\'          => $theCatId,       
    \'posts_per_page\'        => $number_posts,
    \'order\'                 => $order,
    \'orderby\'               => $order_by,
    );  

// The Query
global $wp_query,$paged,$post;
$my_query = new WP_Query( $args );

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

我建议不要让逻辑过于复杂化。你的问题的一般思维过程是正确的。假设当您将meta\\u查询逻辑直接添加到查询并运行它时,以上所有内容都可以正常工作,那么您的代码应该尝试遵循相同的思维模式。

您正在访问全局列表,因此请先列出这些列表。这也将使它们在以后您需要回到您所做的事情时易于参考:

    // Globals.
    global $wp_query, $paged, $post;
现在您获得了简短的代码属性,不鼓励在这里使用extract,因此我建议将其设置为变量:

Note: 您将需要更新引用根据快捷码属性数组提取的变量的位置。在这个例子中,我刚刚将其命名为$sc

    // Shortcode attributes.
    $sc = shortcode_atts(
        array(
            \'pagination\' => \'true\',
            \'query\' => \'\',
            \'category\' => \'\',
            \'tag_name\' => \'\',
            \'custom_field_1\' => \'\',
            \'custom_field_2\' => \'\',
            \'relation_operator\' => \'\',
            \'number_posts\' => \'\',
            \'order_by\'   => \'\',
            \'order\'     => \'\',
        ),
        $atts
    );
接下来,如果满足条件,您希望修改您的WP\\u查询参数,因此请将您的默认查询参数放在后面,$args:

Note: get_query_var 接受一个默认值作为传递的第二个参数,因此可以去掉该条件并使用默认参数集转换为int(这比问题中使用的逻辑快得多,并产生相同的结果[即,与转换为int相比,absint在内部调用intval])。我可能会直接把它放在$args中,而不是为它设置一个变量

    // WP_Query arguments
    $args = array(
        \'paged\'          => ( int ) get_query_var( \'paged\', 1 ),
        \'post_type\'      => array( \'page\', \' post\' ),
        \'post_status\'    => array( \'publish\' ),
        \'category_name\'  => $sc[\'category\'],
        \'tag\'            => $sc[\'tag_name\'],
        \'category__in\'   => $theCatId,       
        \'posts_per_page\' => $sc[\'number_posts\'],
        \'order\'          => $sc[\'order\'],
        \'orderby\'        => $sc[\'order_by\'],
    );
现在,如果满足某些条件,您需要添加条件并修改参数:

Note: 如果愿意,可以使用if(!empty($custom\\u field\\u 1))。我只是觉得($custom\\u field\\u 1)读起来更容易/写起来更快,但这只是个人喜好

    // custom_field_1 is populated.
    if ( $sc[\'custom_field_1\'] ) {

        // custom_field_2 is populated.
        if ( $sc[\'custom_field_2\'] ) {
            $args[\'meta_query\'] = array(
                    \'relation\' => $sc[\'relation_operator\'],
                    array(
                        \'key\'     => $sc[\'custom_field_1\'],
                        \'value\'   => \'\',
                        \'compare\' => \'!=\',
                    ),
                    array(
                        \'key\'     => $sc[\'custom_field_2\'],
                        \'value\'   => \'\',
                        \'compare\' => \'!=\',
                    ),
                )
            );

        // custom_field_2 is not populated.
        } else {
            $args[\'meta_query\'] = array(
                array(
                    \'key\'     => $sc[\'custom_field_1\'],
                    \'value\'   => \'\',
                    \'compare\' => \'!=\',
                ),
            );
        }
    }
现在,您已经根据需要修改了参数,因此请创建查询:

    // Custom query $my_query.
    $my_query = new WP_Query( $args );
我没有看到任何对$theCatId的引用,所以我假设您在脚本的其他地方处理这个问题。也可以比我在示例代码中所做的更有效地编写内容,但不管怎样,这都应该有助于为您指明获得所需结果的正确方向。

以上所有因素加在一起:

    // Globals.
    global $wp_query, $paged, $post;

    // Shortcode attributes.
    $sc = shortcode_atts(
        array(
            \'pagination\' => \'true\',
            \'query\' => \'\',
            \'category\' => \'\',
            \'tag_name\' => \'\',
            \'custom_field_1\' => \'\',
            \'custom_field_2\' => \'\',
            \'relation_operator\' => \'\',
            \'number_posts\' => \'\',
            \'order_by\'   => \'\',
            \'order\'     => \'\',
        ),
        $atts
    );

    // WP_Query arguments
    $args = array(
        \'paged\'          => ( int ) get_query_var( \'paged\', 1 ),
        \'post_type\'      => array( \'page\', \' post\' ),
        \'post_status\'    => array( \'publish\' ),
        \'category_name\'  => $sc[\'category\'],
        \'tag\'            => $sc[\'tag_name\'],
        \'category__in\'   => $theCatId,       
        \'posts_per_page\' => $sc[\'number_posts\'],
        \'order\'          => $sc[\'order\'],
        \'orderby\'        => $sc[\'order_by\'],
    );

    // custom_field_1 is populated.
    if ( $sc[\'custom_field_1\'] ) {

        // custom_field_2 is populated.
        if ( $sc[\'custom_field_2\'] ) {
            $args[\'meta_query\'] = array(
                    \'relation\' => $sc[\'relation_operator\'],
                    array(
                        \'key\'     => $sc[\'custom_field_1\'],
                        \'value\'   => \'\',
                        \'compare\' => \'!=\',
                    ),
                    array(
                        \'key\'     => $sc[\'custom_field_2\'],
                        \'value\'   => \'\',
                        \'compare\' => \'!=\',
                    ),
                )
            );

        // custom_field_2 is not populated.
        } else {
            $args[\'meta_query\'] = array(
                array(
                    \'key\'     => $sc[\'custom_field_1\'],
                    \'value\'   => \'\',
                    \'compare\' => \'!=\',
                ),
            );
        }
    }

    // Custom query $my_query.
    $my_query = new WP_Query( $args );

结束

相关推荐

shortcode get thumbnail size

如何获取短代码缩略图大小?我的代码在函数中获取短代码缩略图大小:function thumb_medium( $atts, $content = null ) { return wp_get_attachment_url( get_post_thumbnail_id( $post_id, \'medium\') ); //or wp_get_attachment_url( get_post_thumbnail_id( $post_id, \'large\') ); //or