Advertisement
Guest User

wpquery

a guest
Apr 2nd, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1.     $search = sanitize_text_field($_POST['s']);
  2.     $region = (isset($_POST['region']) ? sanitize_text_field($_POST['region']) : '' );
  3.     $post_type = sanitize_text_field($_POST['post_type']);
  4.  
  5.     $activite = array();
  6.     foreach($_POST as $p => $i){
  7.         if(strpos($p,'activite') !== false){
  8.             array_push($activite, $i);
  9.         }
  10.     }
  11.  
  12.     $service = array();
  13.     foreach($_POST as $p => $i){
  14.         if(strpos($p,'service') !== false){
  15.             array_push($service, $i);
  16.         }
  17.     }
  18.  
  19.     //echo $search . PHP_EOL . $region . PHP_EOL . $activite . PHP_EOL . $service . PHP_EOL;
  20.     $args = array(
  21.         'post_type' => $post_type,
  22.         's' => $search,
  23.         'order' => 'asc',
  24.         'orderby' => 'title',
  25.         'posts_per_page' => '-1',
  26.         'tax_query' => array('relation' => 'AND')
  27.     );
  28.     if($region != ''){
  29.         array_push($args['tax_query'], array('taxonomy' => 'region',
  30.                                             'field' => 'slug',
  31.                                             'terms' => $region)
  32.                     );
  33.     }
  34.  
  35.  
  36.     // These are the faulty taxonomies
  37.     if(count($activite) > 0){
  38.         array_push($args['tax_query'], array('taxonomy' => 'activite',
  39.             'field' => 'slug',
  40.             'terms' => $activite,
  41.             'operator' => 'IN'));
  42.     }
  43.  
  44.     if(count($service) > 0){
  45.         array_push($args['tax_query'], array('taxonomy' => 'service',
  46.             'field' => 'slug',
  47.             'terms' => $service));
  48.     }
  49.  
  50.     $query = new WP_Query( $args );
  51.     $results = $query->get_posts();
  52.  
  53. -------------------------------------
  54. Args dump with 1 param checked
  55.  
  56. array(6) {
  57.   ["post_type"]=>
  58.   string(8) "vignoble"
  59.   ["s"]=>
  60.   string(0) ""
  61.   ["order"]=>
  62.   string(3) "asc"
  63.   ["orderby"]=>
  64.   string(5) "title"
  65.   ["posts_per_page"]=>
  66.   string(2) "-1"
  67.   ["tax_query"]=>
  68.   array(2) {
  69.     ["relation"]=>
  70.     string(3) "AND"
  71.     [0]=>
  72.     array(4) {
  73.       ["taxonomy"]=>
  74.       string(8) "activite"
  75.       ["field"]=>
  76.       string(4) "slug"
  77.       ["terms"]=>
  78.       array(1) {
  79.         [0]=>
  80.         string(9) "activite1"
  81.       }
  82.       ["operator"]=>
  83.       string(2) "IN"
  84.     }
  85.   }
  86. }
  87.  
  88. --------------------------------------
  89. Args dump with 2 params
  90.  
  91. array(6) {
  92.   ["post_type"]=>
  93.   string(8) "vignoble"
  94.   ["s"]=>
  95.   string(0) ""
  96.   ["order"]=>
  97.   string(3) "asc"
  98.   ["orderby"]=>
  99.   string(5) "title"
  100.   ["posts_per_page"]=>
  101.   string(2) "-1"
  102.   ["tax_query"]=>
  103.   array(2) {
  104.     ["relation"]=>
  105.     string(3) "AND"
  106.     [0]=>
  107.     array(4) {
  108.       ["taxonomy"]=>
  109.       string(8) "activite"
  110.       ["field"]=>
  111.       string(4) "slug"
  112.       ["terms"]=>
  113.       array(2) {
  114.         [0]=>
  115.         string(9) "activite1"
  116.         [1]=>
  117.         string(9) "activite2"
  118.       }
  119.       ["operator"]=>
  120.       string(2) "IN"
  121.     }
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement