Support » Fixing WordPress » Custom Post Type/Custom Date Field Archive

  • Resolved exoticcorpse

    (@exoticcorpse)


    I am using the following code (found here) to display my upcoming events/shows from the custom post type ‘shows’ ordered by custom date field ‘showdate’.

    <?php
    $timecutoff = date("Y-m-d");
    $args = array(
    'post_type' => 'shows',
    'orderby' => 'meta_value',
    'meta_key' => 'showdate',
    'meta_compare' => '>=',
    'meta_value' => $timecutoff,
    'order' => 'ASC'
    );
    $my_query = new WP_Query($args);
    
    if ($my_query->have_posts()) : while ($my_query->have_posts()) :
    $my_query->the_post();
    $eventdate = get_post_meta($post->ID, "showdate", true);
    ?>
    
    <?php if(!isset($currentMonth) || $currentMonth != date("m", strtotime($showdate))){
        $currentMonth = date("m", strtotime($showdate));
    ?>
    <li><?php echo date("F", strtotime($showdate)); ?></li>
    <?php
    }
    ?>
    
    <ul>
        <li>
            <h5><?php echo $showdate; ?></h5>
            <h4><?php the_title(); ?></h4>
            <?php the_content(); ?>
        </li>
    </ul>
    <?php endwhile; else: ?>
    <ul id="shows">
    <li><?php _e('No Events Scheduled! .'); ?></li>
    </ul>
    <?php endif; ?>

    This outputs in the following format:
    OCTOBER
    Show
    Show

    NOVEMBER
    Show
    Show

    I need to paginate this display by month so it shows
    OCTOBER
    Show
    Show

    Next Page – >

    Then on the following page shows November, the next page December and so on.

    Does anybody have any suggestions for how to achieve this?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Would it be adding ‘paged’ to the arguments, with a month comparison?

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    I once made a custom page template for events with monthly Custom Field Date pagination. This was the template (changed for your post type and custom field name): http://pastebin.com/R2wLstZA
    The format for my custom fieds is yyyy-mm-dd (2012-10-19). With this kind of querying you can use normal WordPress pagination functions.

    And this was in my theme’s functions.php:

    function get_post_meta_archive($key='', $post_type='post'){
                    $this_month = date('Y-m').'-01';
    		global $wpdb;
    		// get event posts with $key from current month and onwards
    		$query = "SELECT YEAR(meta_value) AS `year`, MONTH(meta_value) AS `month`, count(ID) as posts FROM $wpdb->posts LEFT JOIN {$wpdb->postmeta} pm ON ($wpdb->posts.ID = pm.post_id) WHERE post_type = '" . $post_type . "' AND post_status = 'publish' AND meta_key = '" . $key . "' AND meta_value >= '" . $this_month . "' GROUP BY YEAR(meta_value), MONTH(meta_value) ORDER BY meta_value ASC";
    		return $wpdb->get_results($query);
    }

    Hope this helps

    Thanks a lot for that! It appears to work perfectly for the current month (including with my $timecutoff comparison, to hide events that have occured), but I am getting a ‘not found’ on page 2.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Is this on a custom page template or in some other theme template file?

    Can you paste and submit the full code of the template file into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    My code just shows the posts that are from the current month (showdate custom field) or the first month after that has any events. I’m not sure what the $timecutoff meta_value does.

    Yeah, it’s on a custom category template.. I’ll play around with it a bit in more basic terms and let you know if that makes a difference. Thanks again

    Just tried it on a fresh installation with default Twenty Eleven theme. My category template exactly as you had in your pastebin. My functions addition exactly as you pasted above. My permalinks are set to default. First page (October) is fine and completely accurate. Still getting as ‘Page not Found’ for Page 2.

    I am using WordPress 3.4.2

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    You can only use this on a custom page template. I just tested it on a category template like category.php or archive.php and it did not work. Why do want to use it on a category template?

    I see! I was ticking my Events into a Category called ‘Shows’ and displaying them via that category, so I could override the template for that category. I see now that was an unnecessary step. Thank you so much!

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    You’re welcome. I’m glad it is working 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Custom Post Type/Custom Date Field Archive’ is closed to new replies.