WordPress.org

Ready to get started?Download WordPress

Forums

meta post search (2 posts)

  1. kokarat
    Member
    Posted 1 year ago #

    I need to query filter by multi checkbox (image)

    Now query work only second parameter (People) when submit.

    How to solve to filter with two parameters.

    HTML (Form submit)

    <input type="checkbox" id="inlineCheckbox1"  name="category[]" value="Planet">
    <input type="checkbox" id="inlineCheckbox2" name="category[]" value="People">

    PHP (Search)

    $category = $_GET['category'];
    
                    $category_planet = $category[0];
                    $category_people = $category[1];
    
                    $argsearch      = array();
                    $argsearch["post_type"] = "projects";
                    $argsearch['showposts'] = -1;
    
                    if( $advtext!="" ){
                        $advtxtQuery ="select ID from $wpdb->posts
                        LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id where {$wpdb->posts}.post_status = 'publish'  ";
                        $where =" (( post_typs LIKE 'projects') AND ( post_title LIKE '%".$advtext."%')) ";
                        $where .=" AND ( $wpdb->postmeta.meta_key = '_cmb_project_category' AND $wpdb->postmeta.meta_value LIKE '%".$advtext."%')";
                        $mypostids = $wpdb->get_col($advtxtQuery." AND ( ".$where." )" );
    
                        if(count($mypostids)>0){
                            $argsearch['post__in']=$mypostids;
                        }else{
                            $argsearch["s"] = $advtext;
                        }
    
                    }
    
                    if( $category_planet!="" ){
                        if(!isset($argsearch["meta_query"])){
                            $argsearch["meta_query"] = array();
                        }
                        $argsearch["meta_query"][] = array(
                                'key' => '_cmb_project_category',
                                'value' => $category_planet,
                                'compare' => 'LIKE'
                        );
    
                    }
    
                    if( $category_people!="" ){
                         if(!isset($argsearch["meta_query"])){
                              $argsearch["meta_query"] = array();
                         }
                         $argsearch["meta_query"][] = array(
                                   'key' => '_cmb_project_category',
                                   'value' => $category_people,
                                   'compare' => 'LIKE'
                         );
    
                    }
    
                    global $wp_query, $post,$wpdb;
                    $query =  new WP_Query($argsearch);
                    while($query->have_posts()):
                        $query->next_post();
                    $project_list = $query->post->ID;

    Thank you.

  2. Scott Fennell
    Member
    Posted 1 year ago #

    There's a lot going on there and I'm not sure what you're trying to accomplish.

    Two things though...

    1) Do you think it would be a good idea to sanitize the $_GET variables before using them in a database query?

    2) You seem to have a habit of trying to do things with variables before checking if they exist. For example, if $advtext does not yet exist, then advtext!="" is going to throw an error.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags