$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Wordpress loop: Show only a Custom Post Type Taxononmy TERM|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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(); ?> 
Post a comment

comment list (0)

  1. No comments so far