Answers for "loop custom post type taxonomy"

PHP
0

post type taxonomy loop wordpress

<?php
// Get all the categories
$categories = get_terms( 'service-category' );

// Loop through all the returned terms
foreach ( $categories as $category ):

    // set up a new query for each category, pulling in related posts.
    $services = new WP_Query(
        array(
            'post_type' => 'services',
            'showposts' => -1,
            'tax_query' => array(
                array(
                    'taxonomy'  => 'service-category',
                    'terms'     => array( $category->slug ),
                    'field'     => 'slug'
                )
            )
        )
    );
?>

<h3><?php echo $category->name; ?></h3>
<ul>
<?php while ($services->have_posts()) : $services->the_post(); ?>
    <li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>

<?php
    // Reset things, for good measure
    $services = null;
    wp_reset_postdata();

// end the loop
endforeach;
?>
Posted by: Guest on October-25-2020
0

custom post type loop with category

// using category slug
 $args = array(  
          'post_type' => 'produto', 
          'posts_per_page' => -1, 
          'tax_query' => array(
            array(
                'taxonomy' => 'produto_category',
                'field'    => 'slug', // term_id, slug  
                'terms'    => 'taeq',
            ),
           )
         );

// using category id
/* $args = array(  
          'post_type' => 'produto', 
          'posts_per_page' => -1, 
          'tax_query' => array(
            array(
                'taxonomy' => 'produto_category',
                'field'    => 'term_id', // term_id, slug  
                'terms'    => 5,
            ),
           )
         );

*/ 

$loop = new WP_Query($args);
Posted by: Guest on October-05-2020

Code answers related to "loop custom post type taxonomy"

Browse Popular Code Answers by Language