I have only the following code in a Wordpress template file, and though there are only two posts in the database, it goes to infinite loop.
$args = array(
'posts_per_page' => '‐1',
'post_type' => 'products',
);
$myProducts = new WP_Query( $args );
// The Loop
while ( $myProducts->have_posts() ) : $myProducts‐>the_post();
echo 'loop body';
endwhile;
// Reset Post Data
wp_reset_postdata();
If I print the $myProducts variable, I can see the two posts there. Why the infinite loop then?
<?php
$params = array(
'posts_per_page' => 5,
'post_type' => 'product'
);
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>
This works fine for me.
Related
Building a Wordpress page template for a custom taxonomy and related custom post type. Within a new WP_Query I need to grab fields from (2) different ACF post object fields; list-staff and list-rep. Code works as expected up to the wp_reset_postdata(); correct amount of results are returned, data within each post is unique up to the point of the reset. After the reset, all data is the same within each post. Code follows, and I'm certain there's a more elegant solution:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php the_field('list_number'); ?>
<?php
$staff = get_field('list_staff');
$rep = get_field('list_rep');
if( $staff ):
// override $post
global $post;
$post = $staff;
setup_postdata( $post );
?>
<?php the_permalink(); ?><?php the_title(); ?>
<?php
endif;
if( $rep ):
// override $post
$post = $rep;
setup_postdata( $rep );
?>
<?php the_field('mkt_co'); ?><?php the_field('mkt_tel'); ?>
<?php
endif;
wp_reset_postdata();
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>
Figured this one out. setup_postdata() is a completely wrong direction for this application. What is correct is documented on the ACF page for "displaying data for multiple post objects." As the article states "Using this method, the $post object is never changed so all functions need a second parameter of the post ID in question." Read more about it here; https://www.advancedcustomfields.com/resources/post-object/. My working code follows:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php
the_field('list_number');
$post_object = get_field('list_staff');
if( $post_object ):
?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<?php echo get_the_title($post_object->ID); ?>
<?php
endif;
?>
<?php
$post_object = get_field('list_rep');
if( $post_object ):
?>
<p><?php the_field('mkt_co', $post_object->ID); ?></span></p>
<?php the_field('mkt_tel', $post_object->ID); ?>
<?php
endif;
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>
I want to display the specific post title/content in the front static page.Remember not all posts just specific. So can anybody guide me how to do that..
Yes you can get specific posts in front page by passing post ids with array in include parameter something like this,
<ul>
<?php
global $post;
$args = array(
'offset'=> 1,
'include' => array(1,2,3) // PASS POST ID IN ARRAY
'post_type' => 'post', );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
<?php the_content(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
Hope this works.
<?php
$titles=array();
$contents=array();
$links=array();
// the query
$the_query = new WP_Query( array(
'posts_per_page' => 3,
));
?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $titles[]=get_the_title(); ?>
<?php $contents[]=get_the_content(); ?>
<?php $links[]=get_the_permalink();?>
<?php endwhile; ?>
and now printed the value in my page wherever i wanted
<?php echo $titles[0]; ?>
<?php echo $titles[1]; ?>
<?php echo $titles[2]; ?>
And same for other declared arrays. :)
I am create post with advance custom field (movie-page.php) I want to create single page(single-movie.php) each post of movie-page.php when I am click on each post it link to full details but I can not do it. Help me please!!!. thanks you.
And here is my code movie-page.php
<?php
/*
* Template Name: Page - Movie Page
*/
?>
<?php get_header(); ?>
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'movie'
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<div class="jol">
<?php the_title($post->ID); ?>
<?php get_post_field('descriptionii',$post->ID ); ?>
<?php the_content() ?>
<?php the_field("descriptionii") ?>
</div>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php get_footer(); ?>
<?php
/*
* Template Name: Page - Movie Page
*/
?>
<?php get_header(); ?>
<?php $args = array(
'posts_per_page' => -1,
'post_type' => 'movie',
'post_status' => 'publish',
);
$posts_array = get_posts($args);
foreach ($posts_array as $key => $value) {
echo "".$value->post_title."</br>";
echo $value->post_content;
}
?>
<?php get_footer();
I'm attempting to loop thought a custom post type (artists), then each category, then another custom post type (story) which has a relationship with the artist.
The issue is, the function <?php echo get_the_title( $story->ID ); ?> in the final loop is returning a title of the Artist many times over, along with the title of the current custom post. I just need the title of the currentent loops post.
<!-- get all the artists -->
<?php
$args = array(
'post_type' => 'artists'
);
$query = new WP_QUERY( $args );
?>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_field('artist_website'); ?></p>
<!-- looping through all the categories -->
<?php
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h2>".$cat->name."</h2>";
?>
<!-- looping through the stories that have a relationship with the artist (select_artist) -->
<?php
$post_type = 'story';
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1, //show all posts
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $cat->slug,
)
),
'meta_query' => array(
array(
'key' => 'select_artist', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123.
'compare' => 'LIKE'
)
)
);
$stories = new WP_Query($args);
?>
<?php if( $stories ): ?>
<ul>
<?php foreach( $stories as $story ): ?>
<!-- Problem echo below -->
<?php echo get_the_title( $story->ID ); ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php } // done the foreach statement ?>
<?php endwhile; wp_reset_postdata(); endif; ?>
</section>
If you're using WP_Query you should be using a while loop and end with the_post() in order to set up the internal variables that would allow get_the_title() to work properly.
As an added measure you could set the $post variable to a $temp variable before your inner loop, then reset back after.
<?php
$stories = new WP_Query($args);
if( $stories->have_posts() ):
global $post;
$temp = $post;
?>
<ul>
<?php
while( $stories->have_posts() ): $stories->the_post();
the_title();
endwhile;
$post = $temp;
?>
<ul>
<?php endif; ?>
If I add to sidebar this code I can't display anything in page.php I try reset query but no result.
<?php
$args = array(
'post_type' => 'post',
'kalba' => 'lt',
'posts_per_page' => 7
);
$new_query = new WP_Query();
$new_query->query($args);
?>
<?php if ($new_query->have_posts()) : ?>
<?php while ($new_query->have_posts()) : the_post(); ?>
<?php $new_query->the_post();?>
<?php the_title(); ?>
<div class="sidebar-line"></div>
<?php endwhile; ?>
<?php endif; ?>
got confuse y you are using
'kalba' => 'lt',
in your argument. wp_query have no any such argument 'kalba'
may be you can try and change your argument lik
$args = array(
'post_type' => 'post',
'posts_per_page' => 7
);
like this
.. hope it works fine
Before loop I add <?php rewind_posts(); ?> and when it work.