0

We have a news section on our website. We have basically 5 companies giving publications:

('ENEFI', 'dmker', 'megakran', 'otp', 'futuraqua')

We would like to list the latest one from each and order them by date.

What is the best way to achieve this?

My first way to go about it is to create 5 queries, each with a 'tag_id' requirement. and use:

$post_ids = wp_list_pluck( $my_query->posts, 'ID' );

Create an array and then use this array to display the posts as requested.

However, when creating the second array, $post_ids gets overwritten, not added to the last one. How can I get around this, and is this the best way to achieve it?

Thanks!

1
  • this looks like a generic PHP question, e.g. how do I merge/combine 2 arrays instead of overwriting them
    – Tom J Nowell
    Commented Jan 10, 2022 at 14:15

1 Answer 1

0

Managed to get what I wanted like so:

// first query
$first_ids = get_posts( array(

    'post_type' => POST_TYPE_KIB_HIREK,
    'posts_per_page' => 1,
    'post_status'  => 'publish',
    'orderby'      => 'date',     // popular - ha a legolvasott
    'tag_id'           => 102,
    'order'        => 'DESC',
    'fields'         => 'ids',
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC'
));
// second query
$second_ids = get_posts( array(
    'post_type' => POST_TYPE_KIB_HIREK,
    'fields'         => 'ids',
    'posts_per_page' => 1,
    'tag_id'           => 434,
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC',
    
));

// third query
$third_ids = get_posts( array(
    'post_type' => POST_TYPE_KIB_HIREK,
    'fields'         => 'ids',
    'posts_per_page' => 1,
    'tag_id'           => 213,
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC',
    
));

// fourth query
$fourth_ids = get_posts( array(
    'post_type' => POST_TYPE_KIB_HIREK,
    'fields'         => 'ids',
    'posts_per_page' => 1,
    'tag_id'           => 122,
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC',
    
));

$post_ids = array_merge( $first_ids, $second_ids, $third_ids, $fourth_ids );

// the main query
$query = new WP_Query(array(
    'post_type' => POST_TYPE_KIB_HIREK,
    'post__in'  => $post_ids, 
    'orderby'   => 'date', 
    'order'     => 'DESC'
));

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
echo the_title().'<br>';
endwhile; endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.