Shortcode Attributes - post__not_in array

I'm trying to put together a shortcode that let'sthe user loop posts but exclude specific ones by id. $atts

I'm trying to put together a shortcode that let's the user loop posts but exclude specific ones by id.

 $atts = array_change_key_case((array)$atts, CASE_LOWER);

    // override default attributes with user attributes
    $custom_atts = shortcode_atts([
                                     'link' => 'off',
                                     'hide' => '',
                                     'only' => '',
                                     'reverse' => '',
                                 ], $atts, $tag);

    $args = array(
        'cat' => 113,
        'post_type' => 'page',
        'posts_per_page' => '-1',
        'order' => 'ASC',
        'post_parent'     => $current_id,
            'post__not_in'    => array($custom_atts['hide'])

    );


    $the_query = new WP_Query( $args );

Then the shortcode would look like:

[customposts hide="7043,7128"]

But for whatever reason it's only hiding the first post listed in the attribute.

Any ideas?

I'm trying to put together a shortcode that let's the user loop posts but exclude specific ones by id.

 $atts = array_change_key_case((array)$atts, CASE_LOWER);

    // override default attributes with user attributes
    $custom_atts = shortcode_atts([
                                     'link' => 'off',
                                     'hide' => '',
                                     'only' => '',
                                     'reverse' => '',
                                 ], $atts, $tag);

    $args = array(
        'cat' => 113,
        'post_type' => 'page',
        'posts_per_page' => '-1',
        'order' => 'ASC',
        'post_parent'     => $current_id,
            'post__not_in'    => array($custom_atts['hide'])

    );


    $the_query = new WP_Query( $args );

Then the shortcode would look like:

[customposts hide="7043,7128"]

But for whatever reason it's only hiding the first post listed in the attribute.

Any ideas?

Share Improve this question asked Jul 27, 2019 at 19:10 WaterbarryWaterbarry 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

With this part:

'post__not_in' => array($custom_atts['hide'])

you're actually running:

'post__not_in' => array( "7043,7128" )

with [customposts hide="7043,7128"].

Note that post__not_in expects an array of numbers, not an array with a single CSV element.

Have a look at e.g. wp_parse_id_list() and don't forget to validate the input, to e.g. avoid empty input or a one with non integers.

PS: This can potentially be eat up your resources for large number of posts:

'posts_per_page' => '-1'

Consider adding a limit there instead.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745291096a4620863.html

相关推荐

  • Shortcode Attributes - post__not_in array

    I'm trying to put together a shortcode that let'sthe user loop posts but exclude specific ones by id. $atts

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信