Skip Navigation

[Resolved] Filter WordPress Archive by a newly added checkbox

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

Problem:

Filter views result by custom checkbox field in custom PHP codes.

Solution:

See examples here:

https://toolset.com/forums/topic/filter-wordpress-archive-by-a-newly-added-checkbox/#post-2223673

Relevant Documentation:

This support ticket is created 3 years 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.

Our next available supporter will start replying to tickets in about 0.53 hours from now. Thank you for your understanding.

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by szymonF 3 years ago.

Assisted by: Luo Yang.

Author
Posts
#2223651

I'm sorry, I don't really understand 🙁 Does this mean I need to use the code with the pre_get_query? Where does it go?
(I'm not that pro 😉 )

#2223673

Hello,

For multiple post views, you can setup the custom codes as below:

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

Please replace 123, 456, 789 with your post view's IDs

More help:
hidden link

For WordPress Archive, you can use pre_get_posts action hook like this:

function WA_display_not_exist_posts($query) {
  if( !is_admin() && $query->is_main_query() && is_post_type_archive( 'movie' ) ) {
    $query->set( 'meta_query', [
      [
       'key'     => 'wpcf-hide-me',
       'compare' => 'NOT EXISTS',
      ],
    ]);
  }
}
add_action( 'pre_get_posts', 'WA_display_not_exist_posts', 99 );

More help:
https://developer.wordpress.org/reference/hooks/pre_get_posts/

#2227165

Thank you! This was super clear and helpful. The views worked great out of the box, and for archives I only had to remove the && is_post_type_archive( 'movie' ), looks like for regular posts this is just not needed at all.