2017-03-07 4 views
0

私のワードプレスでクエリを実装しようとしています。私はこの本/?サイクルのようなURLを持つ2つのフィルタ複数の値のクエリカスタムフィールドWP

  1. "サイクル"
  2. "代わり"

このコードは

<?php if($_GET['cycle'] && !empty($_GET['cycle'])) 
{ 
$cycle = $_GET['cycle']; 
} else { 
} 
if($_GET['lieu'] && !empty($_GET['lieu'])) 
{ 
$lieu = $_GET['lieu']; 
} else { 
} 
?> 

<?php 
       $args = array(
       'post_type' => 'enseignement', 
       'posts_per_page' => 10, 
       'meta_query' => array(
         'relation' => 'AND', 
         array(
          'key' => 'cycle', // name of custom field 
          'value' => $cycle, // matches exactly "red" 
          'compare' => 'LIKE', 
                 ), 
       array(
        'key'  => 'lieu', 
        'value' => $lieu, 
        'compare' => 'LIKE', 

     ), 
    ), 


       ); 
      $loop = new WP_Query($args); 
      while ($loop->have_posts()) : $loop->the_post(); ?> 
      <?php get_template_part('content', 'enseignement', get_post_format());?> 
      <?php endwhile; ?> 

を働くと表示post_type "enseignement" をしたいです= cycle1 & lieu = paris

しかし、複数の「サイクル」または複数の「l ieu "like this /?cycle = cycle1、cycle2 & lieu = paris、marseille私は動作しません。

どうすれば修正できますか?

答えて

0

あなたのURLでこのようなものがある場合:

/?cycle[]=cycle1&cycle[]=cycle2&lieu[]=paris&lieu[]=marseille

をあなたは$_GET['cycle']で、あなた$_GET['lieu']パラメータの配列を取得します。 Visual of an array in a $_GET field

あなたがそうのようなWP_Queryの引数に直接渡すことができます。

$args = array(
    'post_type'  => 'enseignement', 
    'posts_per_page' => 10, 
    'meta_query'  => array(
     'relation' => 'AND', 
     array(
      'key'  => 'cycle', // name of custom field 
      'value' => $_GET['cycle'], // matches any field in the $_GET['cycle'] array 
      'compare' => 'LIKE', 
     ), 
     array(
      'key'  => 'lieu', 
      'value' => $_GET['lieu'], // matches any field in the $_GET['lieu'] array 
      'compare' => 'LIKE', 
     ), 
    ), 
);