맞춤 분류에서 게시물 가져 오기
-
-
세금 쿼리의 'field'에 유효한 값은 'term_id','name'또는 'slug'입니다.valid values for `field` in a tax query are `term_id`, `name`, or `slug`.
- 1
- 2014-10-16
- Milo
-
4 대답
- 투표
-
- 2014-10-16
세금 쿼리가 잘못되었습니다.
field
는term_id
,name
또는slug
-$posts_array = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'fabric_building', 'tax_query' => array( array( 'taxonomy' => 'fabric_building_types', 'field' => 'term_id', 'terms' => $cat->term_id, ) ) ) );
Your tax query is incorrect,
field
should be the field you want to query on:term_id
,name
, orslug
-$posts_array = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'fabric_building', 'tax_query' => array( array( 'taxonomy' => 'fabric_building_types', 'field' => 'term_id', 'terms' => $cat->term_id, ) ) ) );
-
이 게시물을 얻은 후 페이지를 매기는 방법?how to paginate this posts after get their?
- 0
- 2018-09-28
- Andreas Hunter
-
- 2014-10-16
가능한 경우 분류에
get_terms()
를 사용해야합니다.<?php /* Add your taxonomy. */ $taxonomies = array( 'fabric_building_types', ); $args = array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '', 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' ); $terms = get_terms( $taxonomies, $args ); foreach ( $terms as $term ) { // here's my code for getting the posts for custom post type $posts_array = get_posts( array( 'showposts' => -1, 'post_type' => 'fabric_building', 'tax_query' => array( array( 'taxonomy' => 'fabric_building_types', 'field' => term_id, 'terms' => $term->name, ) ) ) ); print_r( $posts_array ); } ?>
Codex 링크 : http://codex.wordpress.org/Function_Reference/get_terms
You should use
get_terms()
for taxonomies when you can.<?php /* Add your taxonomy. */ $taxonomies = array( 'fabric_building_types', ); $args = array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '', 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' ); $terms = get_terms( $taxonomies, $args ); foreach ( $terms as $term ) { // here's my code for getting the posts for custom post type $posts_array = get_posts( array( 'showposts' => -1, 'post_type' => 'fabric_building', 'tax_query' => array( array( 'taxonomy' => 'fabric_building_types', 'field' => term_id, 'terms' => $term->name, ) ) ) ); print_r( $posts_array ); } ?>
Link to Codex: http://codex.wordpress.org/Function_Reference/get_terms
-
고마워요 ...하지만 똑같은 문제가 남아 있습니다 .. 빈 배열 .. 내가 용어 배열을 얻고 있기 때문에 사용자 정의 포스트 유형을 정의하는 데 문제가 있기 때문입니다.Thanks...but the same issue remains..empty array.. is it because i am doing something wrong in defining custom post type as i am getting the term array..
- 0
- 2014-10-16
- Parth Kumar
-
위의 업데이트 된 코드를 아직 시도해 보셨습니까?Have you tried the updated code above yet?
- 0
- 2014-10-16
- Courtney Ivey
-
실제로 Milo가 제안한 것처럼 필드는 문자열을 가져옵니다.Actually as Milo suggested, the field will take a string..that was the error..which i got resolved..
- 1
- 2014-10-16
- Parth Kumar
-
- 2016-09-14
global $post; $id = $post->ID; $cat = get_the_category($id); $loc = get_the_terms($id, 'taxonomy'); $posts = get_posts( array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1, 'category' => $cat[0]->term_id, 'tax_query' => array( array( 'taxonomy' => 'location', 'field' => 'id', 'terms' => $loc[0]->term_id, ) ) ) ); print_r($posts);
효과가있을 것입니다.
global $post; $id = $post->ID; $cat = get_the_category($id); $loc = get_the_terms($id, 'taxonomy'); $posts = get_posts( array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1, 'category' => $cat[0]->term_id, 'tax_query' => array( array( 'taxonomy' => 'location', 'field' => 'id', 'terms' => $loc[0]->term_id, ) ) ) ); print_r($posts);
this should be works.
-
- 2018-06-27
현재 분류에 할당 된 게시물 가져 오기
taxonomy-your_tax.php에 아래 코드를 추가 할 수 있습니다.
<div class="a-article-wrapper"> <?php $terms = wp_get_post_terms( $post->ID, 'your-taxonomy'); $terms_ids = []; foreach ( $terms as $term ) { $terms_ids[] = $term->term_id; } $args = array( 'post_type' => 'your-post-type', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'your-taxonomy', 'field' => 'term_id', 'terms' => $terms_ids ) ), ); $query = new WP_Query($args); if ( $query->have_posts() ) { while ( $query->have_posts() ) { ?> <div class="row"> <div class="col-md-8 a-article-row"> <div class="row"> <?php $query->the_post();?> <div class="a-post-time"> <span class="a-current-date"><?php the_time('j F, D') ?></span> <span class="a-current-time"><?php the_time('g:i a') ?></span> </div> <div class="a-article-title"> <?php the_title(); ?> </div> <div class="a-article-content"> <div id="excerpt"><?php the_excerpt(); ?></div> <?php the_content(); ?> </div> <div class="a-article-tags"> <?php echo get_the_term_list( get_the_ID(), 'your-taxonomy', '', ',' ); ?> </div> </div> </div> </div> <?php } } ?> </div>
Getting posts assigned to current taxonomy
You can add below code in taxonomy-your_tax.php
<div class="a-article-wrapper"> <?php $terms = wp_get_post_terms( $post->ID, 'your-taxonomy'); $terms_ids = []; foreach ( $terms as $term ) { $terms_ids[] = $term->term_id; } $args = array( 'post_type' => 'your-post-type', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'your-taxonomy', 'field' => 'term_id', 'terms' => $terms_ids ) ), ); $query = new WP_Query($args); if ( $query->have_posts() ) { while ( $query->have_posts() ) { ?> <div class="row"> <div class="col-md-8 a-article-row"> <div class="row"> <?php $query->the_post();?> <div class="a-post-time"> <span class="a-current-date"><?php the_time('j F, D') ?></span> <span class="a-current-time"><?php the_time('g:i a') ?></span> </div> <div class="a-article-title"> <?php the_title(); ?> </div> <div class="a-article-content"> <div id="excerpt"><?php the_excerpt(); ?></div> <?php the_content(); ?> </div> <div class="a-article-tags"> <?php echo get_the_term_list( get_the_ID(), 'your-taxonomy', '', ',' ); ?> </div> </div> </div> </div> <?php } } ?> </div>
사용자 지정 분류 (
fabric_building_types
)에서 게시물을받지 못했습니다.cat_id
및cat->name
도 받고 있지만 게시물을 가져올 수 없습니다.누구나 도와 주 시겠어요 ... 미리 감사합니다