Hi there,
I have 2 Custom Post Types with different Custom Fields. If I select the Custom Fields in the Filter the Result is that it only shows one of the custom post types.
Example:
1A. Custom Post Type = Veranstaltung
2A. Custom Field = Startdatum (Date)
1B Custom Post Type = Publikation
2B Custom Field = Ablaufdatum (Date)
In Views I make it like that:
1. I select the Custom Post Types
1A and 1B
2. In the Filter I make it like that:
Show all with Startdatum > Futureday = 10
Show all with Ablaufdatum < Futureday = 20
The Result is, it shows me only one Custom Post Type with the Startdatum and the other CPT with Ablaufdatum is ignored (don't show).
How can I resolve this?
Maybe if I use both CF in all CPT?
Then I have to fill all the CF (Until now I have tausends of Posts), and if its blank, it can't filter it out.
Hope you have some Ideas to help me.
Kind Regards
Adnan
Dear Adnan,
Unfortunately Views does not support this kind of queries, this behavior is expected. But you could do something like that using wpv_filter_query function. See an example below, I will explain it after code:
add_filter('wpv_filter_query', 'items_per_page_func', 10, 2);
function items_per_page_func($query, $settings) {
if($settings['view_id'] = 123)
{
$query['meta_query']['relation'] = 'OR';
$query['meta_query']['key'] = 'wpcf-startdatum';
$query['meta_query']['compare'] = 'NOT EXISTS';
$query['meta_query']['value'] = '';
$query['meta_query']['key'] = 'wpcf-ablaufdatum';
$query['meta_query']['compare'] = 'NOT EXISTS';
$query['meta_query']['value'] = '';
return $query;
}
}
You must replace 123 with the ID of the View, also the name of the custom fields with the right ones. It will display also posts that there are not these custom fields.
It is not a so good workaround, WordPress could change this way, but for now it should works. I will let our development aware about that, so we could implement something in the future.
Dear Adriano,
BIG thank you!!!! Works perfect!!!
Hop the development team could make it happen in the future.
Thanks again for your help.
Kind regards
Adnan