WordPress.org

Ready to get started?Download WordPress

Forums

Display custom post type by custom field date range (9 posts)

  1. braderz31
    Member
    Posted 2 years ago #

    Hi there,

    I've been struggling with this for days now and I've tried using various plugins etc but nothing seems to work.

    I am storing events as custom post types and I have a custom field that stores the event date in YYY-mm-dd format. Each event also has a taxonomy assigned to it.

    I have managed to display my custom posts by taxonomy but I need to display the posts by taxonomy within a certain date range.

    For example display all events that have a taxonomy of 'mountain bike' and are taking place in April.

    This is the code I have so far for the taxonomy part... can anyone advise what I need to do to include the custom date field for the date range part.

    <?php
    query_posts( array(
    'post_type' => 'event',
    'event-category' => 'bmx racing'
    'posts_per_page' => '-1',
        'orderby' => 'date',
        'order' => 'ASC',
         ) );
      if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
  2. Konstantin Kovshenin
    Member
    Posted 2 years ago #

    Hey there, you're currently sorting by date, that's the publish date of the post (or in your case, the event) and has nothing to do with the custom fields that you have used. You should be using a meta_query to select, filter and sort by custom fields value: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

    Also take a look at the "type" parameter, which can be DATE or DATETIME in your case, since you're using YYYY-MM-DD (note that's four Y's, not three) or you can just convert your day to a timestamp and manage all the comparison using numbers, not dates.

    Good luck!

  3. braderz31
    Member
    Posted 2 years ago #

    Hey thank so much for your quick reply.

    Please accept my apologies as I am really new to PHP.

    Are you able to explain how to convert the date that I currently have and then compare them so I can find all posts within a date range

    I've got this so far, but I don't know how to convert the date as you describe above. (Also I get an error with this code)

    <?php
    query_posts( array(
    'post_type' => 'event',
    'event-category' => 'bmx racing'
    'posts_per_page' => '-1',
        'orderby' => 'date',
        'order' => 'ASC',
        'meta_query' => array(
    		    array(
    			'key' => 'event-start-date',
    			'value' => array( 2012-05-01, 2012-05-31 ),
    			'compare' => 'BETWEEN',
    			'type' => 'date',
    			),
         ), ) );
      if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>

    I appreciate your help and patience :-)

  4. braderz31
    Member
    Posted 2 years ago #

    I've now come up with this, but I'm not getting any results...

    <?php $loop = new WP_Query( array(
    		'post_type'         => 'event',
    		'event-category'    => 'bmx racing',
                    'posts_per_page'    =>  -1,
                    'orderby'	    => 'meta_value',
                    'order'             =>  'ASC',
                    'meta_key'          =>  'event-start-date',
                    'meta_value' 	    => array( 2012-05-01, 2012-05-31 ),
    			'compare' => 'BETWEEN',
    			'type' => 'DATE'
                                     ) );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div style="width:100%;"><?php meta('event-start-date'); ?></div>
    	<?php the_title( '<p><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></p>' ); ?>
    
    <?php endwhile; ?>
  5. braderz31
    Member
    Posted 2 years ago #

    Ok, I'm getting there now. I've managed to list all events in the correct order but I need to display the events for a specific month.

    I guess I would need to query the date field to see if it contains the specific month?

    Any ideas how to do this please?

    This is my code

    <?php $loop = new WP_Query( array(
    		'post_type'         => 'event',
    		'event-category' => 'bmx racing',
                    'posts_per_page'    =>  -1,
                    'orderby'	    => 'meta_key',
                    'order'             =>  'ASC',
    		'key'           => 'event-start-date',
                    'value'         => date('Ymd',strtotime("today")),
                    'compare'       => '>=',
                    'type'          => 'DATE'
    						)
                     );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
  6. Konstantin Kovshenin
    Member
    Posted 2 years ago #

    A few more hours and you'll solve it on your own :) When I sad store your start date as a number, I didn't mean "Ymd", I meant a UNIX timestamp, returned by the time() function. You should also orderby meta_value_num not by meta key. Also, key, value, compare and type have to be inside a meta query, and if you're ordering by meta_value_num you need a meta_key argument too. Please, please, please, read this page, carefully, from top to bottom: http://codex.wordpress.org/Class_Reference/WP_Query

  7. braderz31
    Member
    Posted 2 years ago #

    hmm, maybe I'm not nearly there then :-)

    To be honest I'm just hacking my way through this as I'm not a PHP developer. Are you able to provide any code examples to help me, I'm just worried I'm running time.

    Thanks again for all your help.

  8. sheriffderek
    Member
    Posted 2 years ago #

    <?php query_posts('cat=shows&order=desc&orderby=meta_value&meta_key=show_info_date&showposts=4'); ?>
    
    			<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    						<?php echo get ('show_info_artist_name') ?> <span class="grey">|</span>
    
    						<?php echo get ('show_info_venue') ?>. 
    
    						<?php echo get ('show_info_time') ?>  
    
    						<?php echo get ('show_info_age') ?>
    
    						<?php endwhile; ?>
    						<?php else : ?>
    
    							<h2>shows are not feeding properly.</h2>
    
    						<?php endif; ?>
    						<?php wp_reset_query(); ?>

    this is how i have the query sorting the dates from magic fields / or custom fields. this doesn't help with the listing only current month... but i thought at least it would be a simple example. i'm currently trying to find how to only show the 7 upcoming days. (by custom field date)

  9. Kimikaze
    Member
    Posted 1 year ago #

    I'm making a booking system and came across this post.

    Seems like there are only a couple of mistakes in you second posts code.

    'value' => array( 2012-05-01, 2012-05-31 ),
    should be
    'value' => array( '2012-05-01', '2012-05-31' ),

    'event-category' => 'bmx racing'
    should be
    'category_name' => 'bmx-racing',
    if it is a normal category (category_name uses category slug (NOT name))

    You could also try:
    'value' => '2012-05',
    'compare' => 'LIKE',
    'type' => 'CHAR',
    to get that month

Topic Closed

This topic has been closed to new replies.

About this Topic