cgee
cgee

Reputation: 1897

Get three specific wordpress posts and random the output

I've this wordpress get_posts arguments:

    $arg = array(
        'orderby' => 'rand',
        'numberposts' => 1,
        'post_type' => 'dmd_berater',
        'post_name' => 'test1',
        'meta_query' => array(
            'key' => 'dmd_subject',
            'value' => 'Daten und Analysen',
            'compare' => 'LIKE'
        ),
    );
    $consultant_post = get_posts($arg); //contains ALL posts.

The code above contains all posts but I only want the post with the post_name 'test1'.

Is this possible? And how?

It should be great if I can output three specific posts. I think it should be work with an array? Like:

'post_name' => array('test1', 'test2', 'test3'),
  1. Can somebody find my issue?
  2. Is an array possible?

Upvotes: 0

Views: 44

Answers (3)

Noman
Noman

Reputation: 4116

You can do it by using WP_QUERY() and its parameter post_name__in

According to DOCS

'post_name__in' - Preserve post slug order given in the post_name__in array (available since Version 4.6).

$arg   = array(
    'orderby'       => 'rand',
    'numberposts'   => 3,
    'post_type'     => 'dmd_berater',
    'post_name__in' => array('test1', 'test2', 'test3'),
    'meta_query'    => array(
        array(
            'key'     => 'dmd_subject',
            'value'   => 'Daten und Analysen',
            'compare' => 'LIKE'
        )
    ),
);
$query = new WP_Query($args);
if( $query->have_posts() ) :
    while($query->have_posts()) : $query->the_post();
        // Your loop code
    endwhile;
else :
    echo wpautop('Sorry, no posts were found');
endif;

Upvotes: 0

Andy Tschiersch
Andy Tschiersch

Reputation: 3816

'post_name__in' => array('test1', 'test2', 'test3')

See the paramters section for more informations.

Upvotes: 1

hannesvieider
hannesvieider

Reputation: 46

if you want 'test1' is the slug of the post, then use just 'name' => 'test1'.

-->here is the link

return value of get_posts is an array

--> here

Upvotes: 0

Related Questions