Sorting by custom field date and post date?
-
I would like to sort by the date in the custom date picker if present or the publish date if not.
Example:
August 2016
Custom Date PickerMarch 2016
Post Publish DateFebruary 2016
Custom Date PickerI have gotten this to work partially by using the following code:
$queryArgs = array( 'post_type' => 'portfolio', 'meta_key' => 'event_start_date', 'orderby' => 'meta_value_num date', 'order' => 'DESC', );
However, this sorts by custom date then post date. So the result looks like this:
August 2016
Custom Date PickerFebruary 2016
Custom Date PickerMarch 2016
Post Publish DateI spoke to James at ACF and he suggested this might be because they save in two different formats. So I used the following code to determine what formats they save in:
<?php echo get_field('event_start_date') ?> <br> <?php the_date(); ?>
This resulted in:
20160803 March 24, 2016
So I am thinking I either need to save both as a variable with modified formats and then feed them into my loop OR, as James suggested (and which I am utterly confused how to do).
“You can always create a dummy date field for your news and set the published date automatically using the “acf/save_post” hook and the update_field() function. That way, you only need to order by the custom field, which should have the same structure in the database.
I’m not sure how to use either…
https://www.ads-software.com/plugins/advanced-custom-fields/
- The topic ‘Sorting by custom field date and post date?’ is closed to new replies.