Steggie Steggie - 7 months ago 27
PHP Question

Display custom taxonomy in a loop Wordpress

Ok, this is probably an easy one. But I can't seem to figure it out for some reason.

I have a custom post type called: Beachevents.
There i have a couple of events. I also have a custom taxonomy called: Thema.

When making my beachevent pages (not posts) i created some types of thema's (themes). Like: Strand Spellen (the slug is strand-spellen).

Now I want to make a loop that display's only strand-spellen with thumbnail and all that stuff.

Does anyone know how I go about this?

I tryed some codes like these but do don't do the trick.

$args = array(
'post_type' => 'beachevents',
'posts_per_page'=> -1,
'tax_query' => array(
array(
'taxonomy' => 'strand-spellen',
'field' => 'slug',
'terms' => 'all'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>

<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
}


Thanks!

Answer

You're close!

In your tax_query, taxonomy needs to refer to 'beachevents' and terms needs to refer to 'strand-spellen'.

So, your code will look like this:

    'tax_query' => array(
            array(
                'taxonomy' => 'thema',
                'field' => 'slug',
                'terms' => 'strand-spellen'
            )
        )

For more information on building your queries, you may find the WP_Query documentation useful - there's a section in there on taxonomy queries.