最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get Posts by Custom Post Type ,Taxonomy, and Term

programmeradmin3浏览0评论

Ok, so i have a Custom Post Type called "Services". This custom post type has a taxonomy called "Areas" and there's 5 terms in that taxonomy.

Let's say I have 10 posts on "Services" and there's 5 posts in the term "Painting" and 5 more on the term "Photography".

I need to be able to query posts from "Services" but instead of showing those 10 posts, only show 5 associated to "Painting".

At the moment i'm able to query by taxonomy and terms, but that will show all of the posts from "services" with no filter by term.

Basically query post by post_type from the term I choose.

Any help would be awesome. Thanks.

<ul id="service-list">
<?php 
        $args = array('tax_query' => array( array('taxonomy' => 'areas', 'field' => 'slug','terms' => 'painting')));

        $the_query = new WP_Query( $args );

        if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

        ?>

    <li class="service">
        <h2><?php the_title(); ?></h2>
        <?php the_content(); ?>
    </li><!-- /.service -->

<?php endwhile; else: ?>

    <p>Nothing Here.</p>

<?php endif; wp_reset_postdata(); ?>

</ul><!-- #service-list -->

So if I could just specify on the $args from which post type to get the posts from this would be solved.

Ok, so i have a Custom Post Type called "Services". This custom post type has a taxonomy called "Areas" and there's 5 terms in that taxonomy.

Let's say I have 10 posts on "Services" and there's 5 posts in the term "Painting" and 5 more on the term "Photography".

I need to be able to query posts from "Services" but instead of showing those 10 posts, only show 5 associated to "Painting".

At the moment i'm able to query by taxonomy and terms, but that will show all of the posts from "services" with no filter by term.

Basically query post by post_type from the term I choose.

Any help would be awesome. Thanks.

<ul id="service-list">
<?php 
        $args = array('tax_query' => array( array('taxonomy' => 'areas', 'field' => 'slug','terms' => 'painting')));

        $the_query = new WP_Query( $args );

        if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

        ?>

    <li class="service">
        <h2><?php the_title(); ?></h2>
        <?php the_content(); ?>
    </li><!-- /.service -->

<?php endwhile; else: ?>

    <p>Nothing Here.</p>

<?php endif; wp_reset_postdata(); ?>

</ul><!-- #service-list -->

So if I could just specify on the $args from which post type to get the posts from this would be solved.

Share Improve this question edited Oct 15, 2012 at 10:38 hitautodestruct 5652 gold badges7 silver badges19 bronze badges asked Mar 14, 2012 at 17:43 RiseRise 6752 gold badges7 silver badges11 bronze badges 3
  • Ok, so it looks like I over thought all of this and the solution was really simple: – Rise Commented Mar 14, 2012 at 17:55
  • Please mark your answer as correct or delete the topic. – AlxVallejo Commented Mar 14, 2012 at 17:57
  • I need to wait 7 hours to post the solution :( – Rise Commented Mar 14, 2012 at 17:58
Add a comment  | 

1 Answer 1

Reset to default 36

This is the answer to the question :)

<?php 

$args = array(
    'post_type'=> 'services',
    'areas'    => 'painting',
    'order'    => 'ASC'
);              

$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : 
       $the_query->the_post(); 
       // content goes here
    endwhile; 
    wp_reset_postdata(); 
else: 
endif;

?>
发布评论

评论列表(0)

  1. 暂无评论