Fento Fento - 6 months ago 28
PHP Question

php wp-types display taxonomy

I'm using the wp-types plugin to manage custom post types on a wordpress site. To display a list of post types, I use the following code:

<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'products',
'orderby' => 'meta_value',
'post_count' => -1
);
$query = new WP_Query( $args ); ?>

<?php while ( $query->have_posts() ) : $query->the_post(); $categories = get_the_category(); ?>

<div>The custom post</div>



<?php endwhile; wp_reset_postdata(); ?>


is there I way I can modify this to display a list from a particular taxonomy for (in this case) products?

Cheers

Answer

You can, it will look something like this:

<?php 
    $args = array(
        'posts_per_page' => 20,
        'post_type' => 'products',
        'orderby' => 'meta_value',
        'post_count' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'example', // get posts in the 'example' taxonomy
                'field' => 'slug', // that have a slug that matches
                'terms' => 'test', // any of the terms listed
            ),
        ),
    );
    $query = new WP_Query( $args ); ?>

    <?php while ( $query->have_posts() ) : $query->the_post(); $categories = get_the_category(); ?>

    <div>The custom post</div>



    <?php endwhile; wp_reset_postdata(); ?> 

Check out the Taxonomy section of the WP_Query documentation for more information on each of the parameters.