2017-12-26 6 views
0

나는 "시리즈"라는 사용자 정의 필드와 여러 게시물이 있습니다. 이 사용자 정의 필드로 모든 게시물을 그룹화하고 싶습니다.이 아래에이 사용자 정의 필드가없는 모든 게시물을 나열하고 싶습니다.그룹 사용자 지정 값 필드에 의해 wordpress 게시물

먼저 그룹화 된 사용자 정의 필드 값을 얻은 다음이 사용자 정의 값 키와 값을 가진 모든 게시물을 다시 쿼리 할 수 ​​있기를 원합니다. 그러나 고유 한 사용자 지정 값을 얻으려고해도 작동하지 않습니다. 내가 뭘하려

은 이것이다 :

<?php 
    function query_group_by_filter($groupby){ 
     global $wpdb; 

     return $wpdb->postmeta . '.meta_key = "series"'; 
    } 
?> 

<?php add_filter('posts_groupby', 'query_group_by_filter'); ?> 

<?php $states = new WP_Query(array(
    'meta_key' => 'series', 
    'ignore_sticky_posts' => 1 
)); 
?> 

<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?> 


<ul> 
<?php 
while ($states->have_posts()) : $states->the_post(); 

$mykey_values = get_post_custom_values('series'); 

foreach ($mykey_values as $key => $value) { 
    echo "<li>$key => $value ('series')</li>"; 
} 

endwhile; 
?> 
</ul> 

무슨 잘못이 코드? 사용자 정의 값으로

답변

1

WP_Meta_Query

모든 게시물 : 값없이

<?php 
$args = array(
    'post_type' => 'my-post-type', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'meta_query' => array(
     'relation' => 'AND', 
     array(
      'key' => 'series', 
      'value' => 'my-val', 
      'compare' => '=' 
     ), 
     array(
      'key' => 'series', 
      'value' => '', 
      'compare' => '!=' 
     ) 
    ) 
); 

$the_query = new WP_Query($args); 
if ($the_query->have_posts()) : ?> 
<ul> 
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <li id="post-<?php the_ID(); ?>"> 
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
     </li> 
    <?php 
    endwhile; 
    wp_reset_postdata(); ?> 
</ul> 
<?php endif; ?> 

그리고 :

<?php 
$args = array(
    'post_type' => 'my-post-type', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'meta_query' => array(
     array(
      'key' => 'series', 
      'value' => '', 
      'compare' => '=' 
     ) 
    ) 
); 

$the_query = new WP_Query($args); 
if ($the_query->have_posts()) : ?> 
<ul> 
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <li id="post-<?php the_ID(); ?>"> 
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
     </li> 
    <?php 
    endwhile; 
    wp_reset_postdata(); ?> 
</ul> 
<?php endif; ?> 
+0

감사합니다! 그러나 첫 번째 부분 (값을 가진 쿼리)은 내가 직접 검색하는 것이 아닙니다. 먼저 사용자 정의 필드로 그룹이 필요합니다. 예를 들어 사용자 정의 필드 'series'에 의한 그룹은 'Development Series'와 'Cloud Series'라는 값을 가진 게시물을 가지고 있다고 가정하고이 두 가지 변수 사용자 정의 필드 값별로 그룹화 한 다음 해당 그룹 아래에 관련 게시물을 표시하려고합니다. – STORM