WordPress.org

Ready to get started?Download WordPress

Forums

I have simple but urgent issue with limiting results for query_posts (8 posts)

  1. binarybit
    Member
    Posted 4 years ago #

    Hello fellow wordpress gurus!

    I have a real problem which I have not been able to resolve even after 4 hours of trying to get this to work.

    In summary, all I am trying to do is limit the number of results from a query_posts query.

    Whats unique about this query is that I am essentially building an event calendar in which I have gotten wordpress to correctly display event posts based on a custom field called "event_start_date" sorted by upcoming events showing up first and have gotten it to excluding any posts which are older than todays date.

    While the code below works perfect now I just want to limit the number of results to just 3. THIS IS MY ISSUE.

    I am guessing that this would involve combining query or filtering the results through an array but I am not not aware of how this is done.

    If anyone would be so kind and just modify the code below so I know how to do this in the future I would greatly appreciate it!!!

    Thank you so much for you help. Below please find the working code I need to modify to only present the top 3 upcoming entries.

    <?php
    //Get the metadata for each child page
    $today = strtotime(date("m/d/Y"));
    global $wp_query;
    query_posts(array('meta_key'=>event_start_date,'orderby'=>meta_value,'cat'=>25,'order'=>ASC,'posts_per_page'=>-1),array('showposts'=>3)); if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php //Get all Events and run a check against current date
    $show_date = strtotime(get_post_meta($post->ID, 'event_start_date', single)); if($show_date >= $today) { ?>
    
    <div id="homepage-events-list-container-group">
    <?php if(get_post_meta($post->ID, 'event_end_time', single) != ''){ ?>
    <div id="homepage-events-list-container-left">
    <div id="homepage-events-list-date"><?php echo get('event_start_date'); ?></div>
    <div id="homepage-events-list-time"><?php echo get('event_start_time'); ?> - <?php echo get('event_end_time'); ?></div>
    </div>
    
    <?php } else { ?>
    <div id="homepage-events-list-container-left">
    <div id="homepage-events-list-date"><?php echo get('event_start_date'); ?></div>
    <div id="homepage-events-list-time"><?php echo get('event_start_time'); ?></div>
    </div>
    <?php } ?>
    <div id="homepage-events-list-container-right">
    <div id="homepage-events-list-title">"><?php echo the_title(); ?></div>
    <div id="homepage-events-list-excerpt">
    <b><?php echo get('event_type'); ?></b> - <?php echo substr(get_the_excerpt(),0,115); echo '... ' ?></div>
    </div>
    </div>
    <?php } ?>
    
    <?php endwhile; endif; ?>
  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Start by fixing this..

    get_post_meta($post->ID, 'event_start_date', single)
    get_post_meta( $post->ID, 'event_start_date', true )

    ..likewise for the other get_post_meta calls..

    and this..

    query_posts(array('meta_key'=>event_start_date,'orderby'=>meta_value,'cat'=>25,'order'=>ASC,'posts_per_page'=>-1),array('showposts'=>3));
    query_posts(array('meta_key'=>'event_start_date','orderby'=>'meta_value','cat'=>25,'order'=>'ASC','posts_per_page'=>-1),array('showposts'=>3));

    ..non-numeric values should be strings (ie. inside quotes)..

  3. binarybit
    Member
    Posted 4 years ago #

    First of all, thank you very much for the information.

    I have made the applicable changes that you suggested but I am still getting the same exact results as before meaning I am NOT getting just the 3 posts showing up as desired.

    Any other help would be greatly appreciated!

    Here is my code right now:

    <?php
    	//Get the metadata for each child page
            $today = strtotime(date("m/d/Y"));
    	 global $wp_query;
            query_posts(array('meta_key'=>'event_start_date','orderby'=>'meta_value','cat'=>25,'order'=>'ASC','posts_per_page'=>-1),array('showposts'=>3));
    	 if (have_posts()) : while (have_posts()) : the_post(); ?>
    
        <?php  //Get all Events and run a check against current date
            $show_date =  strtotime(get_post_meta($post->ID, 'event_start_date', true));
            if($show_date >= $today) { ?>
    	<div id="homepage-events-list-container-group">
    		   <?php if(get_post_meta($post->ID, 'event_end_time', true) != ''){ ?>
                         <div id="homepage-events-list-container-left">
    			<div id="homepage-events-list-date"><?php echo get('event_start_date'); ?></div>
    			<div id="homepage-events-list-time"><?php echo get('event_start_time'); ?> - <?php echo get('event_end_time'); ?></div>
    			</div>
                       <?php } else { ?>
                         <div id="homepage-events-list-container-left">
    			<div id="homepage-events-list-date"><?php echo get('event_start_date'); ?></div>
    			<div id="homepage-events-list-time"><?php echo get('event_start_time'); ?></div>
    			</div>
                       <?php } ?>
               	<div id="homepage-events-list-container-right">
    		<div id="homepage-events-list-title"><a href="<?php the_permalink() ?>"><?php echo the_title(); ?></a></div>
    		<div id="homepage-events-list-excerpt">
    		<b><?php echo get('event_type'); ?></b> - <?php echo substr(get_the_excerpt(),0,115); echo '...  ' ?></div>
    		</div>
                </div>
        <?php } ?>
    
    <?php endwhile; endif; ?>
  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    query_posts(array('meta_key'=>'event_start_date','orderby'=>'meta_value','cat'=>25,'order'=>'ASC','posts_per_page'=>3,'paged'=>$paged));
  5. binarybit
    Member
    Posted 4 years ago #

    Thank you t31os_ for the information but although I have implemented your suggestion I am not not getting any results.

    Below please find the code being run. Please let me know if I have integrated this incorrectly. Also, I have tried it with the "global $wp_query;" under the $today = strtotime(date("m/d/Y")); which did not change the results.

    Any assistance is greatly appreciate!!!

    <?php
    	//Get the metadata for each child page
    	$today = strtotime(date("m/d/Y"));
    	$paged = get_query_var('paged') ? get_query_var('paged') : 1;
    	query_posts(array('meta_key'=>'event_start_date','orderby'=>'meta_value','cat'=>25,'order'=>'ASC','posts_per_page'=>3,'paged'=>$paged));
    	if (have_posts()) : while (have_posts()) : the_post(); ?>
    
        <?php  //Get all Events and run a check against current date
            $show_date =  strtotime(get_post_meta($post->ID, 'event_start_date', true));
            if($show_date >= $today) { ?>
    	<div id="homepage-events-list-container-group">
    		   <?php if(get_post_meta($post->ID, 'event_end_time', true) != ''){ ?>
                         <div id="homepage-events-list-container-left">
    			<div id="homepage-events-list-date"><?php echo get('event_start_date'); ?></div>
    			<div id="homepage-events-list-time"><?php echo get('event_start_time'); ?> - <?php echo get('event_end_time'); ?></div>
    			</div>
                       <?php } else { ?>
                         <div id="homepage-events-list-container-left">
    			<div id="homepage-events-list-date"><?php echo get('event_start_date'); ?></div>
    			<div id="homepage-events-list-time"><?php echo get('event_start_time'); ?></div>
    			</div>
                       <?php } ?>
               	<div id="homepage-events-list-container-right">
    		<div id="homepage-events-list-title"><a href="<?php the_permalink() ?>"><?php echo the_title(); ?></a></div>
    		<div id="homepage-events-list-excerpt">
    		<b><?php echo get('event_type'); ?></b> - <?php echo substr(get_the_excerpt(),0,115); echo '...  ' ?></div>
    		</div>
                </div>
        <?php } ?>
    
    <?php endwhile; endif; ?>
  6. binarybit
    Member
    Posted 4 years ago #

    BTW - I "think" it is important to note that in my original code I was first doing a call to get ALL of the posts and in the second part I am telling the system to just present the ones based on all posts which have the custom field for the event date...

    I am assuming that the second part might need to be merged into the first query otherwise I guess the correct 3 most upcoming event posts won't show up... am I correct with this?

    How would this be done? Anyone able to just rewrite/organize the code correctly so I can copy and past it in and see if it works?

    Thanks in advance!

  7. vtxyzzy
    Member
    Posted 4 years ago #

    Assuming that you are getting a list of posts in the correct order, you can limit the number with a counter, although that may not be the most efficient way to do it.

    query_posts(array('meta_key'=>'event_start_date','orderby'=>'meta_value','cat'=>25,'order'=>'ASC','posts_per_page'=>3,'paged'=>$paged));
    if (have_posts()) :
       $counter = 0;
       while (have_posts()) : the_post(); ?>
    
          <?php  //Get all Events and run a check against current date
          $show_date =  strtotime(get_post_meta($post->ID, 'event_start_date', true));
          if($show_date >= $today) {
             if (++$counter > 3) break; ?>
             <div id="homepage-events-list-container-group">
  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    It's hard to offer suggestions because there obviously requirements to running your code, there are function calls there not native to WordPress, which means myself, vtxyzzy or whoever can't just plonk your code into a file for testing.

    My advice, put in only the bare essentials, get the key elements working first, then add the HTML / Design elements last ... else you over complicate your code simply out of rushing to add everything in..

    So, strip out everything not needed to test and display the data, and worry about the visual side "once it works"...

    That's generally how i approach it when i'm dealing with fiddly code.. plus the less there is to look at, the easier it is to read and understand..

    Keep printing out variables/arrays/objects as you go, to check it holds the data you're expecting it to..
    eg.

    $somevar = array('val1','val2');
    
    ... more code...
    
    // Make sure the array i'm expecting is an array and has the data i expect it to..
    print '<pre>';
    print_r( $somevar );
    print '<pre>';
    // Output
    Array(
      [0] => val1
      [1] => val2
    )
    // At least at this point i know my array has what it should (that was the point, now i can remove the print lines)

    ...etc..

Topic Closed

This topic has been closed to new replies.

About this Topic