Skip Navigation

[Resolved] How to list post based on non custom field

This thread is resolved. Here is a description of the problem and solution.

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 3 years, 7 months ago. 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.

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)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Pat 3 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#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: English (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

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.