0

I'm using WP_Query with ACF(Advanced Custom Fields)

I'm building a news portal, and I'm trying to pass related stories from 1 post which is top_story = true.

In another WP_Query I want to print the related articles to the post_id which is top_story and the id of that post, which normally can change.

My queries looks like this:

$args = array(
    'posts_per_page' => 1,
    'category_name' => 'News',
    'meta_key' => 'top_story',
    'meta_value' => 1
);
$the_query = new WP_Query( $args );

The secound query where I want to display the related articles from the post_id of the previous query is:

$ids = get_field('related_articles', false, false);
$args = array(

    'posts_per_page'    => 2,
    'post__in'      => $ids,
    'post_type' => 'post',
    'post_id' => $post_id,
    'category_name' => 'News'

I don't know if it's possible to pass the post_id from the 1st WP Query to the 2nd WP Query and print the related stories to that article with that post_id

I will be grateful if someone had the same issue and resolve it somehow

Cheers, Mile

  • NVM I got it working – Mile Milosheski Apr 30 '15 at 8:04
  • how you got it ,, please tell – Amar Singh Jan 5 '16 at 13:11
  • @YoYo hey mate, you should probably build 1 query first to get the top story id's first and put all the top story ids in 1 array. Then while building the exact loop where you list the posts you should just use that array to exclude those post ids from the second query it should go something like this: – Mile Milosheski Jan 6 '16 at 19:33
1

1st query:

$exclude_post_top_stories = '';

// WP_Query arguments
$args = array (
    'post_status'            => array( 'publish' ),
    'posts_per_page'         => '-1',
    'order'                  => 'DESC',
    'orderby'                => 'date',
);

// The Query
$exclude_query = new WP_Query( $args );

// The Loop
if ( $exclude_query->have_posts() ) {
    while ( $exclude_query->have_posts() ) {
        $exclude_query->the_post();
        // do something
    $exclude_post_top_stories[] = get_the_id();
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

2nd query:

$args = array(

    'posts_per_page'    => 2,
    'post_not_in'      => $exclude_post_top_stories,
    'post_type' => 'post',
    'post_id' => $post_id,
    'category_name' => 'News
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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