Date Filter for custom filed

Posted in: Newsmag
Post count: 17

Hi,

I have a CPT with custom fields for events. I want to list upcoming events (based on the event date not the post date) in block 9 on the sidebar.
So I have field “event_startdate” (stored in post_meta table, meta_key) which I want to use for sorting.
The meta-value of this is DATE.

I already add this sort option to the td-config and the switch statement in td_data_source, but it won’t work.
I guess I have to deal with meta_query options, I tried
case ‘event_startdate’:
$wp_query_args['meta_key'] = 'event_startdate';
also this:
$wp_query_args['meta_query'] = array(
'relation' => 'AND',
'events' => array(
'key' => 'event_startdate',
'value' => 'meta_value_DATE',
'type' => 'DATE',
),
);
$wp_query_args['orderby'] = 'events';

It seems that my settings from the meta query gots overruled by the standard sorting.

Thanks for a hint.

Post count: 6600

Hi,

Make sure you use the same implementation we have for the popular and popular7 sorts: http://screencast.com/t/ZdDyngSUWO2c also try to replace those with your own custom filed and also check your filed and see if it contains any values.

Eventually, if you can’t manage this on your own please get in touch with someone to assist you on this becasue we cannot help you with customizations.

Hope this helps!
Thanks for the message!

Post count: 17

Thank you Lucian,

it works now.

Here is ma code, just for reference:

case 'event_startdate':
//set the date of today
$today = date('Y-m-d');
//we're looking for the "event_startdate" in postmeta (based on pods custom field)
$wp_query_args['meta_key'] = 'event_startdate'; //enter your field name
//we want to compare the event_startdate with today
$wp_query_args['meta_value'] = $today; // see declaration above
//field format is DATE
$wp_query_args['meta_type'] = 'DATE';
//we want only events which are in the future
$wp_query_args['meta_compare'] = '>';
$wp_query_args['order'] = 'ASC';
//sort the list based on the event_startdate_value
$wp_query_args['orderby'] = 'meta_value';
break;

Post count: 14

oi-admin, will it be possible for you to share your custom post type and sorting files?
I want to implement something similar and I am trying to do this via plugins but it is not working to perfection.

Post count: 17

Hi aabuday,

I have a custom post type ‘events’. This holds a couple of fields. relevant for sorting is one date field, which is ‘event-startdate’. This one is used as shown in the snippet above.

I’m using the pods.io framework for all the handling of CPTs and custom fields. works great.

If you need further assistance, please give me some more information what exactly you want to achieve.

Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic.