在wordpress中按id获取帖子

Riz*_*Riz 3 wordpress posts

我想通过 id 获取帖子。Id 在数组中。我正在使用这段代码,但现在正在工作。

$the_query = new WP_Query( array( 
    'post_type' => 'job_listing', 
    'post__in' => array( 311, 312 ) 
));

print_r($the_query); //this doesn't print any data

if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
}
Run Code Online (Sandbox Code Playgroud)

Pur*_*iya 8

您可以使用,因为它采用与WP_Queryget_posts()相同的参数。

要向其传递 ID,请使用'post__in' => array(311, 312)(仅采用数组)。

下面是示例。

$args = array(
    'post_type' => 'job_listing',
    'post__in' => array(311, 312)
);

$posts = get_posts($args);

foreach ($posts as $p) :
    //post!
endforeach;
Run Code Online (Sandbox Code Playgroud)