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; ?>   
  • 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 Dec 2 '18 at 22:46
  • I need a query just like the wp_query for, example, 'cat=5' but for custom post type taxonomy. – Jesús Cortés Dec 3 '18 at 8:14
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(); ?>

0

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

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.