WordPress.org

Ready to get started?Download WordPress

Forums

Posts per page missing 2 posts - Custom query (9 posts)

  1. gashface
    Member
    Posted 1 year ago #

    I am running a custom query but when returning posts_per_page, it is always two behind. I am using a custom post type called events, that is pulling in the code to display the posts from the functions file. I also want to implement pagination, will this be a problem?

    Any ideas?

    <?php
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       	//Current exhibitions
       	$args = array(
       	       'post_type' => 'exhibitions',
       	       'orderby'   => 'exhibition_start_date',
       	       'meta_key'  => 'exhibition_start_date',
       	       'order'     => 'ASC',
       	       'posts_per_page' => 6,
       	       'paged' => $paged
       	   );
    
       		$my_query = new WP_Query( $args );
    
          while ( $my_query->have_posts() ) : $my_query->the_post();
               $exhibition_start_date = get_post_meta($post->ID, 'exhibition_start_date', true);
               //Current
    
               if ($exhibition_start_date >= date('Y-m-d') ) {
                   get_exhibition_container();
               }
    
       		endwhile;
    
       wp_reset_postdata();
    ?>

    I can post the custom function from my functions file if need be but it's quite long

    Would there be any reason for it consistensly missing two posts?

  2. s_ha_dum
    Member
    Posted 1 year ago #

    Add var_dump($my_query); right after $my_query = new WP_Query( $args ); and tell me if all of your posts are in there.

  3. gashface
    Member
    Posted 1 year ago #

    Nope still missing two posts?

  4. s_ha_dum
    Member
    Posted 1 year ago #

    Try it without the paging stuff-- the last two $args.

    Also, make sure the missing posts are published, not drafts, and have the 'exhibition_start_date' meta_key.

  5. gashface
    Member
    Posted 1 year ago #

    Yeah I've tried that before. Even when using the default amount of posts and no paging (which consequently isn't working either), it misses out two posts.

    They all have the meta data too.

    Is it possible the problem lies with the original functions in functions.php?

    function get_exhibition_container() {
        global $post;
        ?>
        <section class="exhibition_container clearfix">
    
        <?php the_post_thumbnail('slideshowad', array('class' => 'exhibition')); ?>
    
         <div class="left-column-short">
         <p> 
    
         <?php
         $custom = get_post_custom();
        	?>
    
         <strong>
         <?php echo date("F d, Y",strtotime($custom["exhibition_start_date"][0])); ?> 
    
         -<br/>
    	<?php echo date("F d, Y",strtotime($custom["exhibition_end_date"][0])); ?> 
    
         <?php echo get_post_meta($post->ID, 'exhibition_location', true); ?></strong>
    
           <br/>
          <em> <?php  if($custom["exhibition_start_time"][0]);
            echo get_post_meta($post->ID, 'exhibition_start_time', true);' 
    
            -' ?>
          <?php  if($custom["exhibition_end_time"][0]);
    		  echo get_post_meta($post->ID, 'exhibition_end_time', true); ?></em>
        </div>
         <div class="right-column-long">
    	    <div class="right-text">
    	    <h2 class="caps"><?php echo '<a href="'. get_post_permalink($post->ID) . '">'. $post->post_title .'</a></h2>' ?>
    	    <?php the_excerpt($post->ID) ?>
    	    </div>
        </div>
        </section>
  6. s_ha_dum
    Member
    Posted 1 year ago #

    Is it possible the problem lies with the original functions in functions.php?

    No, not really. You aren't getting that far. We've established that. I'll try to revisit this later.

  7. gashface
    Member
    Posted 1 year ago #

    okay, thanks for the help! I appreciate it.

  8. gashface
    Member
    Posted 1 year ago #

    I just wondered if you'd manage to come up with any more ideas of what this could be?

    Var dump - on further perusal I think it might actually be bringing through all posts? including past exhibitions, so those after the date (so past events) but this is probably because the if statement is after the dump?

    Thanks

  9. s_ha_dum
    Member
    Posted 1 year ago #

    var_dump just dumps the information it doesn't change it. Put var_dump($post); before '$exhibition_Start_date....` and remove everything else. In other words, this:

    while ( $my_query->have_posts() ) : $my_query->the_post();
               var_dump($post);
    endwhile;

    Make sure that all of your exhibitions are there because that determines what we do next.

Topic Closed

This topic has been closed to new replies.

About this Topic