-2

I'm new to PHP and I don't know how write a wp_query for search by category. basically i have a website in which i have posts now i want to search accurate post by its name model and year i also attached the screenshot.

i was searching for code but i didn't find anything only find this code but i didn't know what to do in this. check this website their is a reference searchbar
https://www.thecarconnection.com/

code:

<?php
                        $text=$_GET['car'];
                        
                        $model=$_GET['model'];
                        $year=$_GET['year'];
                        
                        
                        
                        
                        if($text!="" && $model=="" && $year=="" )
                        {
                        
                        $the_query = new WP_Query( array( 
                        'post_type' => 'Posts',
                        'paged' => $paged,
                        'meta_query'=>array(
                             array(
                              'key'=>'prp_ulocation',
                              'value'=>$text,
                              'compare'=>'LIKE'
                                  )        
                           )));
                        }
                        else if($text=="" && $model!="" && $year!="" )
                        {
                            $the_query = new WP_Query( array( 
                            'post_type' => 'BMW',
                            'paged' => $paged,
                            'meta_query'=>array(
                            
                                array(
                                  'key'=>'model',
                                  'type'=>'NUMERIC',
                                  'value'=>array($model,$year),
                                  'compare'=>'BETWEEN'
                                      )        
                               )));
                        }
                        else if($text=="" && $model=="" && $year=="" )
                        {
                            $the_query = new WP_Query( array( 
                                'post_type' => 'Kia',
                                'paged' => $paged,
                                'meta_query'=>array(
                                
                                    array(
                                      'key'=>'model',
                                      'type'=>'NUMERIC',
                                      'value'=>$model,
                                      'compare'=>'<='
                                          )            
                                   ))); 
                        }
                        else if($text!="" && $model=="" && $year=="" )
                        {
                            $the_query = new WP_Query( array( 
                                'post_type' => 'Toyota',
                                'paged' => $paged,
                                'meta_query'=>array(
                                
                                    array(
                                      'key'=>'model',
                                      'type'=>'NUMERIC',
                                      'value'=>$model,
                                      'compare'=>'<='
                                          ),
                                    array(
                                      'key'=>'year',
                                      'value'=>$text,
                                      'compare'=>'LIKE'
                                          )       
                                                   
                                   ))); 
                        }
                        
                        
                        while ( $the_query->have_posts() ) : $the_query->the_post();
                        ?> 
                            

their will be a form

1 Answers1

0

Modify this hook. You can retrieve get params and apply it to wp-query. Don't forget to sanitize params you receive from the request

function custom_search( $query_object )
{
    if( $query_object->is_search() ) {
        // Your wp query params
    }
}

add_action( 'parse_query', 'custom_search' );

Documentation here

ibraimV
  • 26
  • 3