Support

Account

Home Forums General Issues Displaying posts by date picker, and hiding expired.

Solved

Displaying posts by date picker, and hiding expired.

  • Hi,

    I working on a new site, I have custom post types being display and paged with the pagination chaining method outlined here:

    http://wp.tutsplus.com/tutorials/custom-post-type-pagination-chaining-method/

    I would like to have one of the custom post types (events) to be displayed by the ACF date & time picker, and for posts with a date that has passed to be hidden. I have tried the code outlined in the link below, however it doesn’t display anything.

    http://old.support.advancedcustomfields.com/discussion/1346/display-posts-based-on-datepicker-value/p1

    Removing 'meta_value' => $todaysDate, allows posts to be displayed, and in order, however old events are still displayed.

    Is there a way I can get this to work properly?

    Thanks,
    Darren.

  • I found a simple script which allowed me to show upcoming events.

    <?php
    
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    $the_query = new WP_Query( array(
    	'post_type' => 'events',
    	'posts_per_page' => 4,
    	'order' => 'ASC',
    	'orderby' => 'meta_value',
    	'meta_key' => 'start_date',
    	'paged' => $paged
    ) );
    $current_header = '';
    
    while ( $the_query->have_posts() ) :
    $the_query->the_post();
    
    # get the datum for this post
    $temp_date = get_post_meta( get_the_ID(), 'start_date', true );
    
    if (strtotime($temp_date) > strtotime('now')){
    
    	if ( $temp_date != $current_header  ) {
    	$current_header = $temp_date;
    	include 'item-archive.php';
    	}?>
    
    <?php } endwhile;?>

    I also removed the pagination chaining because I couldn’t get it to work with this code.

    I have one tester event that has expired, just to ensure its not showing expired events. I have it set to 4 post per page for testing, however on page 1 it only shows 3 posts, like the hidden event is still being counted.

    I’ve been searching for a good solution for this all day, however I can’t find anything that will make it work.

  • Hi @DarrenMooney

    Have you debuged the data in $the_query with a simple print_r ?

    You may find that only 3 posts are found using your query args.

    Thanks
    E

  • Hi Elliot,

    Thank you very much for the reply.

    Before I noticed this I was still working on this problem, and got the following to work:

    <?php
    $currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
    
    $wp_query = new WP_Query(  array (
        'post_type' => 'events',
        'meta_query'=> array(
            array(
              'key' => 'start_date',
              'compare' => '>',
              'value' => $currentdate,
              'type' => 'DATE',
            )),
        'meta_key' => 'start_date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
    	'posts_per_page' => 12,
    	'paged' => $paged,
        )
    ); ?>

    Its not counting the expired posts and is paging perfectly too.

    Thanks again for the reply,
    Darren.

  • Thanks Darren. Worked for me too 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Displaying posts by date picker, and hiding expired.’ is closed to new replies.