Skip Navigation

[Gelöst] How to list post based on non custom field

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

The issue here is that the user wanted to list out all their posts that do not have a specific custom field.

Solution:

Unfortunately this isn't possible to do through the use of the User interface and as such a custom filter is required.

The code below should be able to resolve this.

add_filter( 'wpv_filter_query', 'display_not_exist_posts', 99, 3 );
function display_not_exist_posts( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 123 ) { // if it is specific view and by default
        $query_args['meta_query'][] = array(
                'key'     => 'wpcf-customfieldslug', 
                   'compare' => 'NOT EXISTS',
        );
    }
    return $query_args;
}

Add this code to the Toolset custom code section in Toolset->Settings->Custom code and activate it.

Change 123 to the ID of your view and wpcf-customfeldslug to the slug of your custom field keeping the wpcf- slug

This support ticket is created vor 4 Jahren, 2 Monaten. There's a good chance that you are reading advice that it now obsolete.

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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Pat vor 4 Jahren, 2 Monaten.

Assistiert von: Shane.

Author
Artikel
#1764223

Pat

Hello,

I'm searching a way to display posts that do not have a specific custom field (no record in the database).
The custom field exist and is used on some posts, but I need to retrieve posts that do not have this field.

Is there an available solution within Toolset?
Regards
Pat

#1764379

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Pat,

Thank you for getting in touch.

In a case like this you will need to write a custom filter for the view to check if the fields exist.

add_filter( 'wpv_filter_query', 'display_not_exist_posts', 99, 3 );
function display_not_exist_posts( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 123 ) { // if it is specific view and by default
        $query_args['meta_query'][] = array(
                'key'     => 'wpcf-customfieldslug', 
                   'compare' => 'NOT EXISTS',
        );
    }
    return $query_args;
}

Please try this and let me know if it helps. Replace the 123 with the ID of your view.

Thanks,
Shane

#1765633

Pat

Many thanks Shane