如何使用 post_type、posts_per_page 和 ID 获取自定义类型的帖子过滤

how to get custom type posts filtering with post_type, posts_per_page and IDs

在我的短代码中,我想从我的 custom_type_post 中获取 posts,但使用 ids post_type 和 posts_per_page

过滤它们

例如[myshortcode type="my_custom_type_post"] - 显示 my_custom_type_post

中的所有 post

[myshortcode type="my_custom_type_post" no_of_posts="3"] - 仅显示 3 posts

[myshortcode type="my_custom_type_post" no_of_posts="1" id="112"] - 显示特定的 post

这是我的代码。当我只发送类型和 posts_per_page 和 ids 但当我想显示所有 post 或 no_of_posts

时它不起作用
// extracting values from shortcode
       extract( shortcode_atts( array (
        'type' => '',
        'no_of_posts' => '',
        'id' => ''
    ), $atts ) );

   $id_array = array_map('intval',explode(',',$id));

$args = array(
    'post_type' => $type,
    'posts_per_page' => $no_of_posts,
    'post__in' => $id_array
);

然后将值传递给 wp_query

$query = new WP_Query( $args );
if( $query->have_posts() ){

/*printing out results*/

}

您必须使用如下条件代码

extract( shortcode_atts( array (
        'type' => '',
        'no_of_posts' => '',
        'id' => ''
    ), $atts ) );

$args['post_type'] = $type;

if($no_of_posts) {
    $args['posts_per_page'] = $no_of_posts;
} else {
    $args['posts_per_page'] = '-1';
}

if($id) {
    $args['post__in'] = array_map('intval',explode(',',$id));
}

我只是根据条件设置参数