Skip Navigation

[Résolu] How to list post based on non custom field

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 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.

Marqué : 

This topic contains 2 réponses, has 2 voix.

Last updated by Pat Il y a 10 mois et 3 semaines.

Assigned support staff: Shane.

Auteur
Publications
#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

Languages: Anglais (English )

Timezone: 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