0

I'm trying to create a custom wordpress query that pulls 3 specific IDs based on 3 different variables that are set using Elliot Condon's brilliant advanced custom fields.

I'm using this to set my variables:

<?php
    $post1 = get_field('post_1');
    $post2 = get_field('post_2');
    $post3 = get_field('post_3');
?>

And I want to pass those variables to a custom query like this:

<?php
    $post_list = array($post1, $post2, $post3);
    foreach( $post_list as $post_id ) :
        query_posts('p='.$post_id);
        while (have_posts()) : the_post();
        // echo the post
        endwhile;
        wp_reset_query();
    endforeach;
?>

However, the above doesn't seem to work and results in a broken page. Anyone have any ideas of how to fix? I'm obviously passing the variables into the query wrong, but I can't figure out how to fix it.

EDIT - SOLUTION

Here's the working updated block. Big thanks to DACrosby! I'm running the query for a custom post type, so I needed to specify which type in the $args.

<div class="row">
    <?php
    $post1 = get_field('related_1');
    $post2 = get_field('related_2');
    $post3 = get_field('related_3');
    $args = array(
        'post__in' => array( $post1, $post2, $post3 ),
        'post_type' => 'work'
    );
    $the_query = new WP_Query( $args );
    ?>
    <?php if ( $the_query->have_posts() ): ?>
    <ul class="case-studies cf fade-in fade-in-3">
        <!-- Basic Projects -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php
                //Get Featured Image URL
                $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                //Get Project Categories
                $terms = get_the_terms( $post->ID, 'type' );
            ?>
            <li class="case-study">
                <img src="<?php echo $feat_image; ?>" alt="<?php the_title(); ?>">
                <a href="<?php the_permalink(); ?>" class="cs-hover">
                    <div class="col-table cs-text">
                        <div class="col-tcell">
                            <span><?php the_title(); ?></span>
                            <span class="divider"></span>
                            <span><?php the_field('description'); ?></span>
                            <?php if(get_field('services')): ?>
                                <ul class="tags">
                                    <?php while(has_sub_field('services')): ?>
                                        <li><?php the_sub_field('service'); ?></li>
                                    <?php endwhile; ?>
                                </ul>
                            <?php endif; ?>
                            <span class="text-link">View Project</span>
                        </div>
                    </div>
                </a>
            </li>
        <?php endwhile; ?>
    </ul>
    <?php endif; ?>
    <?php wp_reset_query(); ?>
</div>

1 Answer 1

1

From the documentation

Caveats query_posts() is only one way amongst many to query the database and generate a list of posts. Before deciding to use query_posts(), be sure to understand the drawbacks.

Alters Main Loop query_posts() is meant for altering the main loop. ...

Secondary Loops To create secondary listings (for example, a list of related posts at the bottom of the page, or a list of links in a sidebar widget), try making a new instance of WP_Query or use get_posts().

An example of WP_Query usage:

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

// The Loop
if ( $the_query->have_posts() ) {
  echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

From there you'll just modify the $args sent to WP_Query. For example:

$args = array(
         'post__in' => array( $post1, $post2, $post3 ),
         'post_type' => 'desired_post_type'
); 
Sign up to request clarification or add additional context in comments.

7 Comments

I tried implementing that, but still can't get it to work. I added my full code block to above... maybe you can spot where I'm going wrong? I really appreciate your help!
I don't just see anything immediately wrong.. Are you getting any errors? Check the error_log file on your server, or display errors through WordPress
Not getting any errors in the log, and the rest of the page is loading just fine. All that's being outputted though is <div class="row">" "</div>
Verify what get_field is returning. I use ACF alot and by default it returns a post object - for this setup you'd want it to return the object ID. It's a setting in the Custom Fields setup section per custom field. What's the output of var_dump($args);?
Yeah, I have it set to return the ID. Here's the results of the var_dump: array(1) { ["post__in"]=> array(3) { [0]=> int(84) [1]=> int(92) [2]=> int(104) } }
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.