Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. <?php
  2. function filter_cars_dalinger()
  3. {
  4.     $post_args = [
  5.         'my_marka' => FILTER_SANITIZE_STRING,
  6.         'my_model' => FILTER_SANITIZE_STRING,
  7.         'my_transmission' => FILTER_SANITIZE_STRING,
  8.         'my_fuel' => FILTER_SANITIZE_STRING,
  9.         'my_price-until' => FILTER_SANITIZE_NUMBER_FLOAT,
  10.         'my_price-after' => FILTER_SANITIZE_NUMBER_FLOAT
  11.     ];
  12.  
  13.     $post = filter_input_array(INPUT_POST, $post_args);
  14.     $paged = get_query_var('paged') ? get_query_var('paged') : 1;        //новое
  15.  
  16.     if( isset ($post) )
  17.     {
  18.         // print_r($post);
  19.         $args = array(
  20.             'numberposts'   => -1,
  21.             'meta_key' => 'carsinfo',
  22.             'post_type' => 'cars',
  23.             'paged' => $paged,                                           //новое
  24.             'tax_query' => array(
  25.                 'relation' => 'AND'
  26.             )
  27.         );
  28.  
  29.         if($post['my_marka'] !== 'full')
  30.         {
  31.             array_push($args['tax_query'], array (
  32.                 'taxonomy' => 'carmake',
  33.                 'field' => 'term_id',
  34.                 'terms' => array($post['my_marka']),
  35.             ));
  36.         }
  37.  
  38.         if($post['my_model'] !== 'full')
  39.         {
  40.             array_push($args['tax_query'], array (
  41.                 'taxonomy' => 'carmake',
  42.                 'field' => 'child_of',
  43.                 'terms' => array($post['my_model']),
  44.             ));
  45.         }
  46.  
  47.         if($post['my_transmission'] !== 'full')
  48.         {
  49.             array_push($args['tax_query'], array (
  50.                 'taxonomy' => 'transmission',
  51.                 'field' => 'term_id',
  52.                 'terms' => array($post['my_transmission']),
  53.             ));
  54.         }
  55.  
  56.         if($post['my_fuel'] !== 'full')
  57.         {
  58.             array_push($args['tax_query'], array (
  59.                 'taxonomy' => 'fuel',
  60.                 'field' => 'term_id',
  61.                 'terms' => array($post['my_fuel']),
  62.             ));
  63.         }
  64.  
  65.         if( !empty( $post['my_price-until'] ) )
  66.         {
  67.             $args['meta_query'][] = array(
  68.                 'key' => 'price',
  69.                 'value' => $post['my_price-until'],
  70.                 'type' => 'numeric',
  71.                 'compare' => '>='
  72.             );
  73.         }
  74.  
  75.         if( !empty( $post['my_price-after'] ) )
  76.         {
  77.             $args['meta_query'][] = array(
  78.                 'key' => 'price',
  79.                 'value' => $post['my_price-after'],
  80.                 'type' => 'numeric',
  81.                 'compare' => '<='
  82.             );
  83.         }
  84.  
  85.         return new WP_Query($args);
  86.     }
  87.     else
  88.     {
  89.         $args = array(
  90.             'numberposts'   => -1,
  91.             'meta_key' => 'carsinfo',
  92.             'post_type' => 'cars',
  93.             'current' => max( 1, get_query_var('paged') ),
  94.            'paged' => $paged                                              //новое
  95.         );
  96.  
  97.         return new WP_Query($args);
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement