0

This is my first time working with custom post types and custom taxonomies so please bare with me if this sounds ridiculous.

I've already created this site in php but now im converting it to wordpress.

I've made the following for wordpress: Custom post type: "all_parks"

Custom taxonomy: "all_park_categories"

Term under all_park_categories (i.e., categories I have made for this custom taxonomy): National Parks, National Forests, National Recreational Areas

I want to query the custom post type by taxonomy term/category and then list them in a table

So far its working, BUT the table isn't printing correctly and I don't know how to group them by their term/category. Ideally I would have 3 different tables (1 for each term/category)

<?php
$args = array(
    'post_type' => 'all_parks',
    'posts_per_page' => -1,
    'orderby' => 'name',
    'order' => 'ASC',
    'style' => 'list',

);


$all_parks = new WP_Query( $args );
while ( $all_parks->have_posts() ) : $all_parks->the_post();

 ?>   

    <table>
    <tr> 
        <tr>
            <th>State</th>
            <th>Park Name</th>
        </tr>

<tr>
        <td><a href="/all-states/<?php the_field('state'); ?>/"><?php the_field('state'); ?></a></td>
        <td> <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> </td>
    </tr>
</table>



<?php    
endwhile;

?> 

enter image description here

1

You can use term_query in WP_Query:

$args = array(
    'post_type' => 'all_parks',
    'posts_per_page' => -1,
    'orderby' => 'name',
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'all_park_categories',
            'field'    => 'slug',
            'terms'    => 'national_parks',
        ),
     ),
);
$all_parks = new WP_Query( $args );

Query all posts with all categories and sort by term or create 3 queries with separate terms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.