admin管理员组

文章数量:1316336

I try to get multiple post by ID. For this I created an array for p but I get only the post for the first ID.

<?php
$args = array(
    'p'                      => array('206', '189'),
    'post_type'              => array( 'product' ),
);

$query = new WP_Query( $args );
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
}
?>

I try to get multiple post by ID. For this I created an array for p but I get only the post for the first ID.

<?php
$args = array(
    'p'                      => array('206', '189'),
    'post_type'              => array( 'product' ),
);

$query = new WP_Query( $args );
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
}
?>
Share Improve this question asked Nov 18, 2020 at 0:21 BryanBryan 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 3

Did you look at the documentation for WP_Query?

The p parameter takes only a single integer.

Use post__in.

$args = array(
    'post__in'    => array( 206, 189 ),
    'post_type'   => 'product',
);

本文标签: Get posts by multiple ID39s (query)