2015-01-06 3 views
1

아래 코드를 작성하여 사용자 정의 포스트 타입 (제품 유형)을 정렬하고자합니다. 내 활성 페이지에 '연료'및 '타이어'대신 '석유'라는 용어가있는 게시물 만 표시됩니다.WP_Query 용어로 카테고리를 정렬하십시오.

$args = array(
    'post_type' => 'Products', 
    'posts_per_page' => -1 
); 

$my_query = new WP_Query($args); 

if ($my_query->have_posts()) { 
    while($my_query->have_posts()) {       
    echo '<div class="custom-post">'; 
    $my_query->the_post(); 
    echo '<p>' . the_content() . '</p>'; 
    the_post_thumbnail(); 
    echo '</div>'; 
    } 
    wp_reset_postdata(); 
} 

// 편집, 3에 대한 응답은 다음과

내가 foreach 루프가 WP_Query 방법에 대한 대체 가정 대답하지만, tax_query 나는 그것을 알고 지금, 나에게 새로웠다로 나는 코덱스에서 그것을 찾았지만 여전히 효과가 없다. 나는 honostly 그것은 나를 잘못 tax_query에 뭔가를 명명 같은 간단한 믿고, 그래서 난 내 분류법 여기에 코드가 표시됩니다 :

function my_custom_taxonomies() { 
    register_taxonomy(
     'product-type', 
     'products', 
     array(
      'label' => 'Type of Product', 
      'rewrite' => array('slug' => 'race-products'), 
      'hierarchical' => true, 
     ) 
    ); 
} 

add_action('init', 'my_custom_taxonomies'); 

그리고 $ 인수에 대한 변경 :

$args = array(
    'post_type' => 'Products', 
    'posts_per_page' => -1, 
    'tax_query' => array( 
     array(
      'taxonomy' => 'product-type', 
      'field' => 'slug', 
      'terms' => 'oil' 
     ), 
    ), 
); 

답변

0

는 시도를 코드 아래 :

<?php 
    $myposts = get_posts(array(
     'showposts' => -1, 
     'post_type' => 'Products', 
     'tax_query' => array(
      array(
      'taxonomy' => 'products_cat', 
      'field' => 'slug', 
      'terms' => 'oil' 
       ); 
     ) 
    ) 
    ); 

    foreach ($myposts as $mypost) { 
      echo $mypost->post_title . '<br/>'; 
      echo $mypost->post_content . '<br/>'; 
      echo get_the_post_thumbnail($mypost->ID); 
    } 
?> 
+0

고정되어있어, 'Oil'과 함께 내 어머니 - 혀 'Olie'를 혼합 한 것이므로 tax_query 정보에 너무 감사드립니다! :) – Abarth

0
$loop = new WP_Query(array(
    'posts_per_page' => 10, 
    'post_type' => 'product_type', 
    'order' => 'DESC', 
    'orderby' => 'id', 
    'post_status' => 'publish', 
    'tax_query' => array(
     array(
      'taxonomy' => 'your_texonomy', 
      'field' => 'slug', 
      'terms' => 'Oil', 
     ), 
    ),)); 
0

당신은 쿼리에서 다음 매개 변수를 시도해야합니다.

$args = array(
'post_status' = 'publish', 
'tax_query' = array(
    'taxonomy' => 'categories', 
    'field' => 'id', 
    'term'  => 'category id here' 
) 
); 
$the_query = wp_query($args);