[Resolved] 2 CPT have different CF

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 3 replies, has 2 voices.

Last updated by Adriano 5 years, 8 months ago.

Assigned support staff: Adriano.

Author
Posts
#229669

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

#229693

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

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.

#229846

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

#229850

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Adnan,

Great, you are welcome!