2017-03-07 6 views
0

내 WordPress에서 쿼리를 구현하려고합니다. 내가 원하는 두 개의 필터여러 값 쿼리 사용자 정의 필드 WP

와 디스플레이 post_type "enseignement"
  1. 이 코드는

    <?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; ?> 
    

    내가이이 /?주기와 같은 URL이 작동

"주기"

  • "대신" = cycle1 & lieu = paris

    그러나 여러 개의 "주기"또는 여러 개의 "l ieu "와 같이 /? 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', 
         ), 
        ), 
    ); 
    
    관련 문제