Tag Archives: Taxonomy
Apr.6
2011
WordPress: Query Posts by Taxonomy Term Value
Here’s a simple way to create a custom query for posts by term value. It’s pretty straight forward but if you see a way to improve it, feel free to let me know.
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php // Define the arguments for the query here. $args=array( 'taxonomy' => 'taxonomy_slug_here', 'term' => 'term_slug_here', 'post_type' => 'custom_post_type_slug_here', 'posts_per_page' => -1, 'caller_get_posts'=> 1, 'orderby'=> 'post_date', 'order'=> 'ASC', ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> Call the post content here <?php endwhile; } wp_reset_query(); ?> |
Mar.29
2011
WordPress Taxonomies: Displaying Terms as Text
While using custom post types on a recent site, I ran into a situation where I wanted to list the terms(taxonomies) associated to a post. The function get_the_terms worked great, but I wanted to list the terms as text and not URLs. The first thing that came to mind was to use “strip_tags”, but that just felt dirty… so here’s a simple solution to display a post’s terms as text and not URLs.
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function get_language_terms($args) { // Get terms for post $terms = get_the_terms( $post->ID , $args ); if ( !empty( $terms ) ): // Loop over each item since it's an array foreach( $terms as $term ) { // Print the name method from $term which is an OBJECT echo $term->name; // Get rid of the other data stored in the object, since it's not needed unset($term); } endif; } ?> |
Then place this inside the loop and specify the argument for which taxonomy you are wanting to list terms for.
1 |
<?php get_language_terms('taxonomy_slug_goes_here'); ?> |
I hope this can help someone else as well!