WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] display 'activities' custom post type odered by activity date (3 posts)

  1. LittleBigThings
    Member
    Posted 5 months ago #

    Hi,

    I am using the following code to display my custom post type 'activities'. It works and displays them ordered by activity date (meta data) and only the future (and today) activities are displayed:

    $yesterday = time() - 86400;
    
    $args = array(
    'post_type' => 'activities',
    'posts_per_page' => -1,
    'meta_key' => 'date_activity',
    'orderby' => 'meta_value_num',
    'order'	=> 'ASC'
    );
    $act_query = new WP_Query( $args );
    
    if ( $act_query->have_posts() ) : ?>
    <?php while ( $act_query->have_posts() ) : $act_query->the_post(); ?>
    
    <?php $activity_date = get_post_meta( $post->ID, 'date_activity', true );
    
    if ( $activity_date > $yesterday ) : ?>
    
    <?php get_template_part( 'content', 'activities' ); ?>
    
    <?php endif; ?>
    
    <?php endwhile; ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>

    It works but I cannot really use paging and 'posts_per_page' directly on it since it extracts all posts: also the older ones, it only won't display them. I found a possible solution using meta_compare but I won't get it working like this:

    $yesterday = time() - 86400;
    
    $args = array(
    'post_type' => 'activities',
    'posts_per_page' => -1,
    'meta_key' => 'date_activity',
    'meta_value' => $yesterday,
    'meta_compare' => '>'
    'orderby' => 'meta_value_num',
    'order'	=> 'ASC'
    );
    $act_query = new WP_Query( $args );
    
    if ( $act_query->have_posts() ) : ?>
    <?php while ( $act_query->have_posts() ) : $act_query->the_post(); ?>
    
    <?php get_template_part( 'content', 'activities' ); ?>
    
    <?php endwhile; ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>

    Anyone any idea why it won't work? It displays actually all 'activities' for some reason...
    Thanks a lot!

  2. bcworkz
    Member
    Posted 5 months ago #

    There's nothing wrong with your query other than a missing comma at the end of 'meta_compare'. I assume it's just a typo since you would otherwise get a WSOD instead of all posts.

    I have similar data on my site. To test your query I only changed the post type and meta key to match my data and changed the template loaded.(and added the missing comma) The results were exactly as expected.

    I think most likely you are not storing a true time stamp as meta data. Try var_dump(get_post_meta(get-the_ID(), 'date_activity', true)); on your template. You should see a string representation of a long integer. If you see a date string, then this is the problem.

    Working with date strings in SQL is a pain. If possible consider transitioning to a true time stamp.

  3. LittleBigThings
    Member
    Posted 5 months ago #

    Thanks!
    The comma is indeed a typo (I had to retype the code since I only had the first version).

    Thanks for the tip about var_dump(). It actually worked, just like you said. The dates are stored as a true timestamp. The problem was that I still had a couple of activities with an incorrect date value (displayed as 01/01/1970). These were sorted incorrectly. I have changed their dates, everything works fine now! :-)

Reply

You must log in to post.

About this Topic