wp query - Wordpress loop: Show only a Custom Post Type Taxononmy TERM

admin2025-06-05  1

Before I start, I have to say that I have tried to solve my problem in a thousand different ways and I have read several posts without success.

I have a CPT called "grouped", a taxonomy for that CPT called "orders" and within the WordPress panel I have created several "categories" within orders (terms).

I have a term called "Madrid", and I want to show the post of that term on a page-template. How can I do it? It is killing me literally.

I appreciate your help.

-- This is the loop and the code I used in a normal "taxonomy-template.php" and its work fine, but now I need to show the same information but only showing the post associated with the term "Madrid" in a page template.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php              
        $full = get_the_post_thumbnail_url(get_the_ID(),'full'); 
        $large = get_the_post_thumbnail_url(get_the_ID(),'large');          
        $medium = get_the_post_thumbnail_url(get_the_ID(),'medium');
        $thumbnail = get_the_post_thumbnail_url(get_the_ID(),'thumbnail'); 
    ?>

    <div class="col col-md-flex-6 col-lg-flex-3 ">

        <picture class="cat-agrupados-img">
        <source media="(min-width: 768px)" srcset="<?php echo esc_url($thumbail); ?>">
                <img src="<?php echo esc_url($medium);?>" alt="<?php the_title(); ?>">
        </picture>  

        <h2 class="cat-agrupados-titulo-pedido">Pedido agrupado en <?php the_title(); ?></h2>

        <p class="cat-fecha-publicacion">Fecha de publicación: <strong><?php the_date(); ?></strong></p>

        <p class="cat-agrupados-zona">Zonas: <strong><?php echo get_post_meta( get_the_ID(), 'yourprefix_agrupados_poblaciones', true ); ?></strong></p>


        <a href="<?php the_permalink(); ?>" class="cat-agrupados-link">Ver pedido agrupado</a>


    </div> <!--cierre columna-->

<?php endwhile; endif; ?>   

Before I start, I have to say that I have tried to solve my problem in a thousand different ways and I have read several posts without success.

I have a CPT called "grouped", a taxonomy for that CPT called "orders" and within the WordPress panel I have created several "categories" within orders (terms).

I have a term called "Madrid", and I want to show the post of that term on a page-template. How can I do it? It is killing me literally.

I appreciate your help.

-- This is the loop and the code I used in a normal "taxonomy-template.php" and its work fine, but now I need to show the same information but only showing the post associated with the term "Madrid" in a page template.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php              
        $full = get_the_post_thumbnail_url(get_the_ID(),'full'); 
        $large = get_the_post_thumbnail_url(get_the_ID(),'large');          
        $medium = get_the_post_thumbnail_url(get_the_ID(),'medium');
        $thumbnail = get_the_post_thumbnail_url(get_the_ID(),'thumbnail'); 
    ?>

    <div class="col col-md-flex-6 col-lg-flex-3 ">

        <picture class="cat-agrupados-img">
        <source media="(min-width: 768px)" srcset="<?php echo esc_url($thumbail); ?>">
                <img src="<?php echo esc_url($medium);?>" alt="<?php the_title(); ?>">
        </picture>  

        <h2 class="cat-agrupados-titulo-pedido">Pedido agrupado en <?php the_title(); ?></h2>

        <p class="cat-fecha-publicacion">Fecha de publicación: <strong><?php the_date(); ?></strong></p>

        <p class="cat-agrupados-zona">Zonas: <strong><?php echo get_post_meta( get_the_ID(), 'yourprefix_agrupados_poblaciones', true ); ?></strong></p>


        <a href="<?php the_permalink(); ?>" class="cat-agrupados-link">Ver pedido agrupado</a>


    </div> <!--cierre columna-->

<?php endwhile; endif; ?>   
Share Improve this question edited Dec 3, 2018 at 11:15 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Dec 2, 2018 at 20:49 ctrljctrlj 112 bronze badges 2
  • If I understood correctly, you need to do a custom query that returns the grouped post_type and the orders taxonomy with value Madrid. – Alvaro Commented Dec 2, 2018 at 22:46
  • I need a query just like the wp_query for, example, 'cat=5' but for custom post type taxonomy. – ctrlj Commented Dec 3, 2018 at 8:14
Add a comment  | 

2 Answers 2

Reset to default 0

I found the solution by myself, I leave it here in case someone is interested:

<?php  $args = array(
'post_type' => 'agrupados',
'tax_query' => array(
    array(
        'taxonomy' => 'pedidos',
        'field'    => 'slug',
        'terms'    => 'ferrol',
    ),
),);  $the_query_slide = new WP_Query( $args );  if ( $the_query_slide->have_posts() ) {while ( $the_query_slide->have_posts() ) {$the_query_slide->the_post();?>





            <?php
            /* variables para cada imagen */
                $full = get_the_post_thumbnail_url(get_the_ID(),'full'); 
                $large = get_the_post_thumbnail_url(get_the_ID(),'large');          
                $medium = get_the_post_thumbnail_url(get_the_ID(),'medium');
                $thumbnail = get_the_post_thumbnail_url(get_the_ID(),'thumbnail'); 
            ?>

    <div class="col col-md-flex-6 col-lg-flex-3 ">

        <picture class="cat-agrupados-img">
        <source media="(min-width: 768px)" srcset="<?php echo esc_url($thumbail); ?>">
                <img src="<?php echo esc_url($medium);?>" alt="<?php the_title(); ?>">
        </picture>  

        <h2 class="cat-agrupados-titulo-pedido">Pedido agrupado en <?php the_title(); ?></h2>

        <p class="cat-fecha-publicacion">Fecha de publicación: <strong><?php the_date(); ?></strong></p>

        <p class="cat-agrupados-zona">Zonas: <strong><?php echo get_post_meta( get_the_ID(), 'yourprefix_agrupados_poblaciones', true ); ?></strong></p>


        <a href="<?php the_permalink(); ?>" class="cat-agrupados-link">Ver pedido agrupado</a>


    </div> <!--cierre columna-->



<?php }} wp_reset_postdata(); ?>

In your code you print posts based on global $wp_query object. If you want to print some posts on another template, not using global query, then you'll have to create your custom query object:

<?php 
    $groupped = new WP_Query( array(
        'post_type' => 'grouped',
        'tax_query' => array(
            array(
                'taxonomy' => 'orders',
                'field'    => 'slug',
                'terms'    => 'madrid',
           ),
        ),
    ) ); 
    while ( $groupped->have_posts() ) :
        $groupped->the_post();

        $full = get_the_post_thumbnail_url(get_the_ID(),'full'); 
        $large = get_the_post_thumbnail_url(get_the_ID(),'large');          
        $medium = get_the_post_thumbnail_url(get_the_ID(),'medium');
        $thumbnail = get_the_post_thumbnail_url(get_the_ID(),'thumbnail'); 
?>

    <div class="col col-md-flex-6 col-lg-flex-3 ">

        <picture class="cat-agrupados-img">
        <source media="(min-width: 768px)" srcset="<?php echo esc_url($thumbail); ?>">
                <img src="<?php echo esc_url($medium);?>" alt="<?php the_title(); ?>">
        </picture>  

        <h2 class="cat-agrupados-titulo-pedido">Pedido agrupado en <?php the_title(); ?></h2>

        <p class="cat-fecha-publicacion">Fecha de publicación: <strong><?php the_date(); ?></strong></p>

        <p class="cat-agrupados-zona">Zonas: <strong><?php echo get_post_meta( get_the_ID(), 'yourprefix_agrupados_poblaciones', true ); ?></strong></p>


        <a href="<?php the_permalink(); ?>" class="cat-agrupados-link">Ver pedido agrupado</a>


    </div> <!--cierre columna-->

<?php endwhile; wp_reset_postdata(); ?> 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749132649a316637.html

最新回复(0)