2017-02-08 1 views
1

먼저 내가 알기보다는 지금까지 내 목표를 말합니다.WordPress (중첩) 게시물 쿼리, 범주별로 반복 한 다음 ACF 단위로

내 목표 : 나는 VisualComposer 작동하는 단축 코드를했습니다

  • .
  • VC에서이 게시물 유형의 카테고리를 확인할 수 있습니다.
  • 짧은 코드의 목표는 선택한 카테고리 당 게시물을 선택하고 정렬하는 것입니다.
  • 둘째, ACF 당 나누기 및 정렬. 'custom_field_1'(이)라고 말하십시오.
  • 세 번째로 마지막입니다. 게시물을 "custom_field_2"에 알파벳 순으로 정렬 한 다음 "custom_field_3"에 정렬합니다.

예 1

  • 적색

    • 고양이
      • 후 1
      • 후 2
    • 블루
      • 후 3
      • 후 4
  • 고양이 2
    • 적색
      • 후 5
      • 후 6
    • 블루
      • 후 7
      • 후 8

알 오른쪽. 나는 그것을 먼저 시도했다. 그리고 지금까지 내 코드입니다 :

<?php 
function productlist_sc($atts){?> 

    <?php 
    global $post; 

    $categories_array = array(); 
    $thecategories = get_categories(); 
    foreach($thecategories as $category){ 
     $categories_array[] = $category->slug; 
    } 

    if($atts[ 'category' ]){ 
     $atts[ 'category' ] = explode(",", $atts[ 'category' ]); 
    } 

    //collect values, combining passed in values and defaults 
    $values = shortcode_atts(array(
      'category' => '' 
     ),$atts); 

    $categories = get_categories(array(
      'orderby' => 'name', 
      'parent' => 0, 
      'slug' => $values['category'] 
     )); 

    $current = get_the_ID($post->ID); 
    $cargs = array(
     //'child_of'  => 0, 
     'orderby'  => 'name', 
     'parent' => 0, 
     'order'   => 'ASC', 
     'hide_empty' => 1 
     //'taxonomy'  => 'category', //change this to any taxonomy 
    ); 

    // first sort all selected posts per category 
    foreach ($categories as $tax) : 



     // List posts by the terms for a custom taxonomy of any post type 
     $args = array(
      'post_type' => 'products', 
      'orderby' => 'ASC', 
      'posts_per_page'=>-1, 
      'category_name' => $tax->slug 
     ); 


    $the_query = new WP_Query($args); ?> 
      <?php if ($the_query->have_posts()) : ?> 

       <h2>Internal ID: <?php echo $tax->name; ?></h2><?php 

    // second sort all selected posts per custom_field_1 
    $mykey_values = get_post_custom_values('custom_field_1'); 
    foreach ($mykey_values as $key => $value) : 
     ?><h3><?php the_field('custom_field_1'); ?></h3> 

        <div class="rtt_prod_table"> 

       <!-- the loop --> 
       <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 

          <div> 
           <?php if(get_field('custom_field_1') || get_field('custom_field_2') || get_field('custom_field_3')): ?> 
            <ul> 
             <?php if(get_field('custom_field_2')): ?> 
              <li> 
               <?php the_field('custom_field_2'); ?> 
              </li> 
             <?php endif; ?> 
             <?php if(get_field('custom_field_3')): ?> 
              <li> 
               <?php the_field('custom_field_3'); ?> 
              </li> 
             <?php endif; ?> 

            </ul> 
           <?php endif; ?> 
          </div> 

         </div> 
        </div> 

        <?php endwhile; ?> <!-- end of the loop --> 

       <div><!-- end .accord-content --> 
      </div> 
      <?php wp_reset_postdata(); 
    endforeach; // custom_field_1 
?> 
      <?php else : ?> 
       <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
      <?php endif; ?> 

      <?php 

    endforeach; /* end $tax */ ?> 

<?php 
} 

답변

0

좋아, 그래서 대답을 찾았습니다. 해결책을 찾는 사람을 위해 여기에 게시하겠습니다.:)

단계 :

  • 은, 각 게시물에 대한 사후 쿼리를 실행 "필드 1"
  • 내의 모든 고유 값 배열을 필요로 "필드 1"의 값을 잡고와에 넣어
: 배열
  • 필터링 된 모든 중복
  • 배열에 foreach 문을 실행 배열을 알파벳 순서 6,

    내가

    $stack = array(); 
    
    // query 
    $args = array(
        'post_type' => 'products', 
        'orderby' => 'ASC', 
        'posts_per_page'=>-1, 
        'category_name' => $tax->slug 
    ); 
    
    // The Query 
    $the_query = new WP_Query($args); 
    
    // The Loop 
    if ($the_query->have_posts()) { 
        while ($the_query->have_posts()) { 
         $the_query->the_post(); 
    
         if(get_field('custom_field_1')): 
          $stack[] = get_field('custom_field_1'); 
         endif; 
        } 
        wp_reset_postdata(); 
    } 
    
    $result = array_unique($stack); 
    sort($result); 
    
    foreach ($result as $tax2) : 
        // run you code here 
    endforeach; 
    

    위의 코드에 추가 코드는 좋은 하루 남자와 여자을 가지고있다이!

  • 관련 문제