• Resolved ncox85

    (@ncox85)


    I know this is a common problem, but I am not familiar enough with PHP to adapt any of the many examples to fit my custom loop. I have sorted my loop to display one category called Classes. I am also sorting posts by date using custom fields. But the pagination shows the same posts on each page. I know the query_posts is what is breaking it, but I don’t know how to fix it.

    Here is my loop code:

    <?php if ( have_posts() ) : ?>
    
    	<?php
    	$todaysDate = date('m/d/Y H:i:s');?>
    
    <?php query_posts('showposts=5&category_name=classes&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    
    	<?php
    	get_template_part( 'content-home', get_post_format() );
    		?>
    
    	<?php endwhile; ?>
    
    	<?php _s_paging_nav(); ?>
    
    	<?php else : ?>
    
    	<?php get_template_part( 'content', 'none' ); ?>
    
    	<?php endif; ?>
Viewing 15 replies - 1 through 15 (of 16 total)
  • Take a look at the $paged variable that you’ll want to pass into your query_post call.

    This is an example directly out of the codex:

    <?php
    //Protect against arbitrary paged values
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    
    $args = array(
    	'posts_per_page' => 5,
    	'category_name' => 'gallery',
    	'paged' => $paged,
    );
    
    $the_query = new WP_Query( $args );
    ?>
    <!-- the loop etc.. -->

    the $paged variable tells WordPress what page your on when sorting through the pagination.

    Thread Starter ncox85

    (@ncox85)

    Thanks so much for the reply. This will work to limit the loop to one category, but my bigger problem is how to use the same $args method to sort by date. I dont know how to transfer the code I have now into the array, or if I need to do it a different way.

    Thread Starter ncox85

    (@ncox85)

    Here is my extremely amateur attempt at inserting this code and implementing the date sorting. The pagination now works, but the date sorting is not functioning at all. Also, it is showing more than 5 posts per page, even though the posts_per_page variable is set to 5. How far off am I?

    <?php
    //Protect against arbitrary paged values
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    $todaysDate = date('m/d/Y H:i:s');
    
    $args = array(
    	'posts_per_page' => 5,
    	'category_name' => 'classes',
    	'paged' => $paged,
    	'meta_key' => 'date',
    	'meta_value' => $todaysDate,
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    );
    
    $the_query = new WP_Query( $args );
    ?>

    What’s happening when you run the loop? Are the correct posts being displayed? I don’t believe you need to pass the meta_value parameter into the query if your simply trying to sort them by date.

    Thread Starter ncox85

    (@ncox85)

    The posts are a list of events. The loop is displaying the posts with most recent posting date first. What I am trying to do is to display them like a calendar list, so the posting date will not be the date of the event. I want to sort by the date of the event, which will be posted days or weeks beforehand, and not necessarily in the order they will occur. I am using the custom fields section on the posts to define the date and time of the event, and the first bit of code above I took from a tutorial on how to sort posts using that custom field. Sorry, I should have explained that better.

    The site is currently being hosted on one of my old domains for development if you want to take a look.

    https://www.ccs-strategies.com

    Thread Starter ncox85

    (@ncox85)

    I still can’t figure out why this doesn’t work. My custom field key is Date, and the value is formatted mm/dd/yy 00:00:00. I am trying to order the posts by the date in the custom field, which is the meta_value. What am I missing?

    <?php
    //Protect against arbitrary paged values
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    $todaysDate = date('m/d/Y H:i:s');
    
    $args = array(
    	'posts_per_page' => 5,
    	'category_name' => 'classes',
    	'paged' => $paged,
    	'meta_key' => 'date',
    	'meta_value' => $todaysDate,
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    );
    
    $the_query = new WP_Query( $args );
    ?>

    Have a look at meta_value_num instead of meta_value parameter when comparing numbers in a query. Also if your date is being retreived in m/d/Y format, why not try converting it to an easier to sort format?

    strtotime( $todaysDate );

    and

    'orderby' => 'meta_value_num',

    I actually just set up one of these on a client site myself, so if you need some help feel free to reach out.

    Thread Starter ncox85

    (@ncox85)

    I am not sure where I would insert the strtotime function. I am a very basic beginner at PHP. Also, would I just substitute meta_value for meta_value_num?

    This is my attempt at implementing your suggestions. It did not change anything on the site.

    <?php
    //Protect against arbitrary paged values
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    $todaysDate = date('m/d/Y H:i:s');
    strtotime( $todaysDate );
    
    $args = array(
    	'posts_per_page' => 5,
    	'category_name' => 'classes',
    	'paged' => $paged,
    	'meta_key' => 'date',
    	'meta_value_num' => $todaysDate,
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC',
    );
    
    $the_query = new WP_Query( $args );
    ?>

    You could do:

    $todaysDate = strtotime( date('m/d/Y H:i:s') );

    and the query

    $args = array(
    	'posts_per_page' => 5,
    	'category_name' => 'classes',
    	'paged' => $paged,
    	'meta_key' => 'date',
    	'meta_value_num' => $todaysDate,
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC',
    );

    and then use $todaysDate in the query

    Thread Starter ncox85

    (@ncox85)

    It is still not sorting. Something about the array is not quite right.

    This code was working, but pagination was broken. I guess my question at this point is how do I translate this code into an array, or insert the $paged variables into this code string?

    <?php query_posts('showposts=5&category_name=classes&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?>

    If you store the query into a variable ( ie $query_var = query_posts(etc) ) the new $query_var variable will contain the query array and all post data.

    that being said WP_Query is far more flexible and stable than query_posts() or get_posts()

    Thread Starter ncox85

    (@ncox85)

    Sorry. I don’t understand that at all.

    I think I made a little progress with the code below. It is sorting correctly with the code below, except it is still showing all posts on the same page, even though I have it set to show only 5 posts per page.

    <?php
    				$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    				$todaysDate = date('m/d/Y H:i:s');
    				?>
    
    <?php query_posts('paged=' . $paged . 'posts_per_page=5&category_name=classes&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?>

    Its because your missing the ampersand (&) before ‘posts_per_page’ ,

    it should read &posts_per_page=5

    Corrected Query

    <?php query_posts('paged=' . $paged . '&posts_per_page=5&category_name=classes&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?>

    Thread Starter ncox85

    (@ncox85)

    Working great now. Thanks so much for your help!

    Thread Starter ncox85

    (@ncox85)

    .

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Pagination problem – displays the same posts on each page’ is closed to new replies.