Resetting WordPress post loop - php

I'm trying to get a page ID before and after a post loop, the page ID is displaying correctly before the loop, but when I try and display it after the loop it just displays the last post ID of the loop.
I've tried using all of the following to reset the loop and none seem to work:
wp_reset_postdata()
wp_reset_query()
rewind_posts()
The loop code is:
<?php
echo 'Shows the page ID (correct)=' . $post->ID;
$args = array(
'post_type' => 'accommodation',
'posts_per_page' => '9999',
);
$wp_query = new WP_Query( $args );
if ($wp_query->have_posts()) :
while($wp_query->have_posts()) :
$wp_query->the_post(); ?>
<option value="<?php the_ID(); ?>"><?php echo the_title(); ?></option>
<?php endwhile;
else :
esc_html_e('No bookings','sohohotel');
endif;
wp_reset_postdata();
echo 'Shows the ID of the last post in the loop (not correct)=' . $post->ID; ?>
Note I'm using WP_Query and not query_posts, so I'm not sure where I'm going wrong, any ideas greatly appreciated!

use this
$wp_query = new WP_Query( $args );
if ($wp_query->have_posts()) : $wp_query->the_post();
while($wp_query->have_posts()) :
$wp_query->the_post(); ?>
<option value="<?php the_ID(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else :
esc_html_e('No bookings','sohohotel');
endif;

Related

Wordpress PHP loop custom post type and display on homepage

I'm currently working on a WordPress site and I've created a custom post type called 'events' using the CPT UI plugin.
I want to display the events on my home page, so I've tried to create a loop in my homepage template in the theme files. I've been using this as a guide https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
but for the life of me, I can't get the PHP that is used in that link to work for me.
<?PHP
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
When I try to save that I get this error
syntax error, unexpected 'else' (T_ELSE)
I've been searching for an answer for this for a while and I can't find anything.
I'm pretty new to PHP, so sorry if I'm being incredibly stupid. Any help would be appreciated :)
You have not end while loop , place this code also <?php endwhile; ?>
<?php
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Changing html output structure for a product query in Wordpress

I am using a query that I found on the woocommerce docs, it is a sample products loop. It output's everything as list-items when I want them to outputted into divs with a grid column class. Is there a way to do that with this products loop or do I have to follow another approch?
here is the code so far
<?php
$params = array(
'posts_per_page' => 5, //No of product to be fetched
'post_type' => 'product'
);
$wc_query = new WP_Query($params);
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) :
$wc_query->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
wp_reset_postdata();
else: ?>
<p><?php _e( 'No Products' );?></p>
<?php endif; ?>
This should solve your problem.
<?php
$params = array(
'posts_per_page' => 5, //No of product to be fetched
'post_type' => 'product'
);
$wc_query = new WP_Query($params);
if ($wc_query->have_posts()) :
echo '<div class="your-main-grid-class-or-container">';
while ($wc_query->have_posts()) :
$wc_query->the_post();
?>
<?php echo '<div class="your-child-class">'; ?>
<?php the_title(); ?>
<?php echo '</div>'; ?>
<?php
endwhile;
echo '</div>'; //ending main grid class
wp_reset_postdata();
else: ?>
<p><?php _e( 'No Products' );?></p>
<?php endif; ?>

Can't reset query to show ramdom post after firs loop Wordpress

I have a piece of code to show a random post under a post. But this code only show me the post's write from the author of that post. Whats wrong?
<!-- post -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>
<?php the_author(); ?>
<?php the_category(none); ?>
<?php the_date(); ?>
<?php the_content();?>
<?php $key="video"; echo get_post_meta($post->ID, $key, true); ?>
<?php $key="imagen"; echo get_post_meta($post->ID, $key, true); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<!-- ramdom post -->
<?php $posts = $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) { ?>
<?php the_post_thumbnail('photo-thumbnail') ?>
<?php the_author(); ?>
<?php the_category(none); ?>
<?php } ?>
Try adding <?php wp_reset_query(); ?> after the first query.
Replace the random post code with the code below. In your example you were querying 3 posts. If that's not your intention and you only want a single post replace 3 with 1 in posts_per_page.
$secondary_posts = new WP_Query( array( 'orderby' => 'rand', 'posts_per_page' => 3, 'no_found_rows' => 1, ) );
if ( $secondary_posts->have_posts() ) : while ( secondary_posts->have_posts() ) : $secondary_posts->the_post();
the_post_thumbnail( 'photo-thumbnail' );
the_author();
the_category();
endwhile; endif;
wp_reset_postdata();

exclude category from wordpress post

I want to exclude category from shoowing my blog posts. My category id is 62. category name is perfect_work
Here is my wordpress blog template code:
<div id="left" class="eleven columns">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="title">
<h2><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>" ><?php the_title(); ?></a></h2>
<div class="postmeta"> <span>by <?php the_author_posts_link(); ?></span> | <span><?php the_time('l, F jS, Y') ?></span> | <span><?php the_category(', '); ?></span> </div>
</div>
<div class="entry">
<?php $image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'top_feature'); ?>
<img src="<?php echo $image_attr[0]; ?>" class="postim scale-with-grid" id="blog-thumb" >
<?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
<div class="clear"></div>
</div>
</div>
<?php endwhile; ?>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>
I already tried using
$wp_query = new WP_Query('cat=-62');
its not work. I also put
<?php query_posts('cat=-62'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Its work but page navigation not work, and also not showing others post. only 1st 5 post show.
Any Solution?
Get the page number
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
Then you may use
$wp_query = new WP_Query('cat=-62&paged=' . $paged);
Or use
$cat_id = get_cat_ID('perfect_work');
$wp_query = new WP_Query('cat=-' . $cat_id . '&paged=' . $paged);
Then loop
if($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
// ...
endwhile;
endif;
Try this one you have to specify the showposts to limit the posts
<?php $wp_query->set( 'cat', '-62' ); ?>
<?php query_posts( 'showposts=10' ); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
.
.
.
<?php endwhile; ?>
<?php endif; ?>
Note : The minus sign indicates the exclusion of all Posts which
belong to that category from being retrieved from the database. In
turn, the Loop will never have Posts of that category id and only
process the specified number of Posts of other category ids.
Please read the codex on WP_Query, it is imo very detailed, look at the category params part
Just add a minus sign - in front of the categories you dont want, so the below code would mean show posts with category 10 and 11, but exclude category 62
$recent = new WP_Query("showposts=3&cat=10,11,-62")
You don't need to use the $temp variable before or after the query. You should use something like this:
//This should do the trick
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => -62,
'paged' => $paged
);
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
//The real trick!
<?php wp_reset_postdata(); ?>
Two things to note:
The paged query parameter
To reset the query use wp_reset_postdata()

Php WordPress only show one post

I need this php code to show the most recent post only. Maybe an if statement to show on post. Any ideas? Any help is appreciated.
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<a href="<?php the_permalink() ?>" <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php the_post_thumbnail("events-thumb"); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo nl2br(get_post_meta($post->ID, 'proj_address', true)); ?></p>
<span></span>
<div style="clear:both;"></div>
</a>
<?php endwhile; ?>
<?php
endif;
$wp_query = $temp; //reset back to original query
?>
The $args in this line is where you put any parameters for your query:
$wp_query = new WP_Query($args);
So you can add your post limit there:
$wp_query = new WP_Query( 'numberposts=1' );
(More WP query parameters in the Codex)
I updated your code to get the latest post.
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish'
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<a href="<?php the_permalink() ?>" <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php the_post_thumbnail("events-thumb"); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo nl2br(get_post_meta($post->ID, 'proj_address', true)); ?></p>
<span></span>
<div style="clear:both;"></div>
</a>
<?php endwhile;
endif;
$wp_query = $temp; //reset back to original query
?>
function home_post_limit( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 1 );
}
}
add_action( 'pre_get_posts', 'home_post_limit' );

Categories