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

Gary Stewart
Gary Stewart
14,142 Points

Order WP_Query by meta_value string date?

Hi everyone so I'm using a custom post type 'votes' and am using the Advanced Custom Fields plugin to add additional fields to store data, one of which is a date. The data is stored as a string (long text)

meta_key of completion_date

stored in the format of 2015-01-09 16:48:37

I am wondering what to add to the following query to pull only posts which have a completion date of this current month, ones which have a completion date within this year.

And finally a way to order query posts by this string date?


$args = array (
    'post_type'              => 'votes',
    'meta_query'             => array(
        array(
            'key'       => 'status',
            'value'     => 'Completed',
        ),
    ),
);

$query = new WP_Query( $args );

I have read lots of posts online about it but am getting confused now as some seem that it can be done without modifictions and others mention the strtotime() functions.

Please help! Would really appreciate it as its one of the last few bumps in my project!

Thanks in advance!

4 Answers

Nathalie C
Nathalie C
2,594 Points

Are you still stuck? I've done some messing with a custom events page I'm working on and I think I've managed to get it to display the posts that match this current month...

If you set your custom loop to get the votes with a status of completed, set your custom date field as the 'orderby' value to sort by that date rather than the default post creation date, and then pass those results through another check...

    <?php

    $args = array (
     'post_type' => 'votes',
    'meta_query' => array(
                                     array( 'key' => 'status',
                                                'value'     => 'Completed',
                                              ),
                                    ),
    'orderby' => 'completion_date'
    );

    $the_query = new WP_Query( $args );

    ?>


    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>


    <?php
    $current_month = date ('Ym');
    $complete_date = DateTime::createFromFormat('Ymd', get_field('completion_date'));
    $month = $complete_date->format('Ym');
    if ( $month === $current_month) : ?>

                                     do your stuff here

    <?php endif; ?>

Basically it's getting today's date, as the current month and year (so you won't pull posts from January this year and January last year). Then it gets the date from your custom date field 'completion_date' and formats it to just the month and year. Finally, compare the two, and if they match, set what you want to do with the post. If it doesn't match, no data shown. To query just the year, change the format from 'Ym' to 'Y'.

Nathalie C
Nathalie C
2,594 Points

Can I just ask to clarify, are you asking for the 'post completion date' to be compared to the current month and year?

And this to be one query, i.e. a) posts where completion dates match current month AND current year,

rather than

b) two queries i.e. posts where completion dates match current month OR posts where completion dates match current year?

How do you want to order the posts with this date, in ascending or descending order?

Gary Stewart
Gary Stewart
14,142 Points

Hi Nathalie, thank you for your help!

So to clarify when a vote is ended I create a date using:

current_time('mysql',1); 

This date is then stored within my database as a string in a longtext format.

There are a number of functions I need to run which return various results.

1.) I need to get vote posts from the database which have the the meta_key 'status' as complete (Which i have done in the example above), but I would like to order these posts by the string date of those posts with the meta key completion_date.

2.) The next function, I need the Wp_query to return all posts where that same string completion date is equal to this current month.

3.) Another which is the same as above but for this current year.

Hope that makes sense, really appreciate your help and I'm completely stuck!

Thanks :)

Nathalie C
Nathalie C
2,594 Points

And of course not forgetting to close the main loop at the end

    <?php endwhile; ?>
    <?php else: ?>
    <?php endif; ?>
Gary Stewart
Gary Stewart
14,142 Points

That's brilliant thanks so much for the help!