0

I'm working on a news website that has a top section with eight featured articles. I use ACF fields for these featured articles and input their ID. This is how the loop looks:

$kiemelt1 = get_field('kiemelt_1', 'option');
$kiemelt2 = get_field('kiemelt_2', 'option');
$kiemelt3 = get_field('kiemelt_3', 'option');
$kiemelt4 = get_field('kiemelt_4', 'option');
$kiemelt5 = get_field('kiemelt_5', 'option');
$kiemelt6 = get_field('kiemelt_6', 'option');
$kiemelt7 = get_field('kiemelt_7', 'option');
$kiemelt8 = get_field('kiemelt_8', 'option');

$kiemeltek = array($kiemelt1, $kiemelt2, $kiemelt3, $kiemelt4, $kiemelt5, $kiemelt6, $kiemelt7, $kiemelt8);

$args = array(
    'posts_per_page' => 8,
    'post__in' => $kiemeltek,
);
$wp_query = new WP_Query( $args );

if (have_posts()) {
    $i = 0;
    while ( have_posts() ) {
        the_post();
        $i++;

   if ($i == 1) { 
       //Standard html for $kiemelt1
   }
   
   if ($i == 2) { 
       //Standard html for $kiemelt2
   }
   
   .... etc.

Now, this populates the section fine, and it works for the most part, but the issue arises when I update the array through the options page.

Example:

I exchange the ID in $kiemelt1 and $kiemelt2. The articles load, but the order is not updated. Why can this be the case?

What I tried:

I tried changing the conditional so that aside from $i being a specific number, I double-check for the ID as well:

if ($i == 1 && get_the_id($kiemelt1) ) { } for example, but this causes the layout to break.

Thanks!

2
  • So I managed to fix this issue by creating 8 separate loops, but I am unsure why this was necessary. I created the array $kiemeltek, so I would think that the loop would go through: $kiemeltek[0], $kiemeltek[1], $kiemeltek[2] etc. But it does not, would there have been a better way to do this other than separate queries?
    – Pbalazs89
    Commented Jan 2, 2023 at 12:47
  • 1
    assign the order in your $args.
    – rudtek
    Commented Jan 2, 2023 at 15:44

1 Answer 1

1

As documented, if you're using post__in to query a specific list of posts and what the results returned int he same order you need to set orderby to post__in:

$args = array(
    'posts_per_page' => 8,
    'post__in'       => $kiemeltek,
    'orderby'        => 'post__in',
);

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.