1 comment

  1. Because you want the results of the filtering process be the main object of your page, you may need to alter the main query using pre_get_posts action hook. It is really better that leave the main query run, load your theme and then say to WordPress: go back and run another query as suggested in other answers.

    //Functions for filters
    add_action( 'pre_get_posts', 'my_pre_get_post' );
    function my_pre_get_post($query){
    
         //limit to main query, frontend and archive pages. You need another set of checkings, like check if the request is for your custom post type
         if($query->is_main_query() && !is_admin() && $query->is_archive ) {
    
              $meta_query = array();
              $meta_input = isset($_POST['meta_input']) ? $_POST['meta_input'] : '';
    
              if(!empty($meta_input)){
                  $meta_query[] = array(
                      'key'  => 'your_meta_key',
                      //sanitize $_POST['meta_input'] according with your data type
                      'value'    => $_POST['meta_input'],
                  );
              } 
              $query->set('meta_query',$meta_query);
        }
    
    }
    

    See more information about the meta_query.

Comments are closed.