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 badges1 Answer
Reset to default 0With 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
评论列表(0条)