Reputation: 145
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>
Upvotes: 0
Views: 1948
Reputation: 11440
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().
// 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'
);
Upvotes: 1