0

Currently the user can search for: Type, Make, Mileage [Min, Max] and color. He can search only one, or more than one, or all, as he prefers. I started developing for WP a short time ago, the code I was able to write is functional, but not very scalable.

Code:

$s                                         = $_GET['s'];
if (isset($_GET['carro_marca']))
$search_marca                              = $_GET['carro_marca'];
else
$search_marca                              = '-1';

if (isset($_GET['carro_tipo']))
$search_tipo                               = $_GET['carro_tipo'];
else
$search_tipo                               = '-1';
if (isset($_GET['carro_kmmin']))
$search_kmmin                              = $_GET['carro_kmmin'];
else
$search_kmmin                              = '-1';

if (isset($_GET['carro_kmmax']))
$search_kmmax                              = $_GET['carro_kmmax'];
else
$search_kmmax                              = '-1';

if (isset($_GET['carro_cor']))
$search_cor                                       = $_GET['carro_cor'];
else
$search_cor                                       = '-1';

$minAndMax == false;

if ($search_cor != '-1' ) {
    $temcor = array(
            'key'       => 'carro_cor',
            'value'     => $search_cor,
            'compare'   => 'IN',
        );
}


if ($search_tipo != '-1' ) {
    $temtipo = array(
                'key' => 'carro_tipo',
                'value' => $search_tipo,
                'compare' => '='
        );
} 
if ($search_marca != '-1') {
    $temmarca = array(
                'key' => 'carro_marca',
                'value' => $search_marca,
                'compare' => '='
        );
} 
if ($search_kmmin != '-1' && $search_kmmax != '-1') {
   $minAndMax = array(
                'key' => 'carro_km',
                'value' => array( $search_kmmin,$search_kmmax ),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
    );
} 
if ($search_kmmin != '-1' && $search_kmmax == '-1') {
   $minAndMax = array(
                'key' => 'carro_km',
                'value' => array( $search_kmmin, 10000000000000 ),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
    );
} 
if ($search_kmmin == '-1' && $search_kmmax != '-1') {
   $minAndMax = array(
                'key' => 'carro_km',
                'value' => array( '0',$search_kmmax ),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
    );
} 

//Daqui p Baixo
//tipo
if ($temcor == '-1' && $search_tipo != '-1' && $search_marca == '-1' && $minAndMax == false){
    echo "entrou >:(";
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array('relation' => 'AND', $temtipo)
) ;
}
//marca
elseif ($temcor == '-1' && $search_tipo == '-1' && $search_marca != '-1' && $minAndMax == false){
    echo "entrou >:(";
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array('relation' => 'AND', $temmarca)
) ;
}
//cor
elseif ($temcor != '-1' && $search_tipo == '-1' && $search_marca == '-1' && $minAndMax == false){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array('relation' => 'AND', $temcor)
) ;
}
//MinMax
elseif ($temcor == '-1' && $search_tipo == '-1' && $search_marca == '-1' && $minAndMax == true){
    echo "entrou >:(";
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array('relation' => 'AND', $minAndMax)
) ;
}
//MinMax, Tipo
elseif ($temcor == '-1' && $search_tipo != '-1' && $search_marca == '-1' && $minAndMax == true){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temtipo, $minAndMax)


) ;
//MinMax, Marca
}elseif ($temcor == '-1' && $search_tipo == '-1' && $search_marca != '-1' && $minAndMax == true){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temmarca, $minAndMax)
) ;
}
//MinMax, Cor
elseif ($temcor != '-1' && $search_tipo == '-1' && $search_marca == '-1' && $minAndMax == true){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temcor, $minAndMax)
) ;
}
//tipo, Cor
elseif ($temcor != '-1' && $search_tipo != '-1' && $search_marca == '-1' && $minAndMax == false){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temcor, $temtipo)
) ;
}
//marca, Cor
elseif ($temcor != '-1' && $search_tipo == '-1' && $search_marca != '-1' && $minAndMax == false){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temcor, $temmarca)
) ;
}
//marca, tipo
elseif ($temcor == '-1' && $search_tipo != '-1' && $search_marca != '-1' && $minAndMax == false){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temtipo, $temmarca)
) ;
}
//MinMax, Marca, Tipo
elseif ($temcor == '-1' && $search_tipo != '-1' && $search_marca != '-1' && $minAndMax == true){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temtipo, $temmarca, $minAndMax)


) ;
//MinMax, Cor, Tipo
}elseif ($temcor != '-1' && $search_tipo != '-1' && $search_marca == '-1' && $minAndMax == true){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temtipo, $temcor, $minAndMax)
) ;
}
//Marca, Cor, Tipo
elseif ($temcor != '-1' && $search_tipo != '-1' && $search_marca != '-1' && $minAndMax == false){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temtipo, $temcor, $temmarca)
) ;
}
//MinMax, Cor, Marca
elseif ($temcor != '-1' && $search_tipo == '-1' && $search_marca != '-1' && $minAndMax == true){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temmarca, $temcor, $minAndMax)
) ;
//temAll
}elseif ($temcor != '-1' && $search_tipo != '-1' && $search_marca != '-1' && $minAndMax == true){
$args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'AND', $temcor, $temtipo, $temmarca, $minAndMax)


) ;
}else {
    $args = array(
        'post_status' => 'publish',
        'post_type' => 'carros',
        'posts_per_page' => 10
    );
};

$loop = new WP_Query( $args );

Basically, I created a WP_QUERY for each possibility.

Do you know any better way to write?

Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.