Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development Custom Post Type Templates in WordPress The Portfolio Homepage

Rebecca Jensen
Rebecca Jensen
11,580 Points

How to add additional parameters?

I am trying to add more parameters to my custom post type so that I can display my posts in a custom order, namely by the "event date" that I set and not the publish date. However, whenever I add parameters beneath the post_type parameter used in this example, it breaks the page. Any suggestions on how to add parameters to this?

Here is my code without added parameters:

<div>
    <table class="table table-striped">
        <tr id="column-titles">
           <th>DATE</th>
           <th>EVENT PAGE</th>
           <th class="hidden-xs">TERRAIN</th>
           <th class="hidden-xs">MAP</th>
           <th>DIRECTIONS</th>
           <th>HOST</th>
        </tr>

        <?php

        $args = array(
            'post_type' => 'event'


        );
        $query = new WP_Query( $args );

        ?>


        <?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
                <tr class="event">

                    <td class="date"><?php $date = DateTime::createFromFormat('Ymd', get_field('event_date')); echo $date->format('D, M j');?></td>          <td class="event-name"><a href="<?php the_permalink();?>"><?php the_title();?></a></td>
                    <td class="terrain hidden-xs">Terrain</td>
                    <td class="map-name hidden-xs">Shoreview Community College</td>
                    <td class="event-location">Event location</td>
                    <td class="club-host-name">COC</td>

                </tr>
        <?php endwhile; endif; wp_reset_postdata(); ?>

    </table>
</div>

And this is with the parameters I've tried:

        <?php

        $args = array(
            'post_type' => 'event'
            'meta_key'      => 'event_date',
            'orderby'       => 'meta_value_num',
            'order'         => 'ASC',
            'meta_value'    => date('Ymd'),
            'meta_compare'  => '>=',
                'meta_query'    => array(
                    array(
                            'key' => 'date',
                            'value' => date('Ymd'),
                            'compare' => '>=', //Greater than
                            'type' => 'DATETIME'
                        )
                    ),
            ));
        $query = new WP_Query( $args );

        ?>

Thank you!

Rebecca Jensen
Rebecca Jensen
11,580 Points

I changed meta_query to date_query and now it works!

        <?php


        $query = new WP_Query( array(

            'post_type' => 'event',
            'meta_key'   => 'event_date',
            'orderby'    => 'meta_value_num',
            'order'      => 'ASC',
            'meta_value'    => date('Ymd'),
            'meta_compare'  => '>=',
            'date_query'    => array(
                array(
                        'key' => 'date',
                        'value' => date('Ymd'),
                        'compare' => '>=', //Greater than
                        'type' => 'DATETIME'
                    )
                ),

            ) );

        ?>