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

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Pat 1 year ago.

Assigned support staff: 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