Obtener Entradas Bajo Taxonomía Personalizada

16 oct 2014, 19:31:09
Vistas: 183K
Votos: 43

No estoy pudiendo obtener las entradas bajo la taxonomía personalizada (fabric_building_types). Estoy obteniendo cat_id y cat->name pero no puedo obtener las entradas.

$args = array(
    'type'                     => 'post',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'fabric_building_types',
    'pad_counts'               => false 
); 

$categories = get_categories( $args );

foreach ( $categories as $cat ) {

    // aquí está mi código para obtener las entradas del tipo de post personalizado

    $posts_array = get_posts(
        array(
            'showposts' => -1,
            'post_type' => 'fabric_building',
            'tax_query' => array(
                array(
                    'taxonomy' => 'fabric_building_types',
                    'field' => $cat->cat_ID,
                    'terms' => $cat->name,
                )
            )
        )
    );
    print_r( $posts_array ); 
}

¿Alguien puede ayudarme por favor? Gracias de antemano

1
Comentarios

los valores válidos para field en una consulta de taxonomía son term_id, name o slug.

Milo Milo
16 oct 2014 20:23:42
Todas las respuestas a la pregunta 4
1
81

Tu consulta de taxonomía es incorrecta, field debe ser el campo sobre el que quieres consultar: term_id, name, o slug -

$posts_array = get_posts(
    array(
        'posts_per_page' => -1,
        'post_type' => 'fabric_building',
        'tax_query' => array(
            array(
                'taxonomy' => 'fabric_building_types',
                'field' => 'term_id',
                'terms' => $cat->term_id,
            )
        )
    )
);
16 oct 2014 20:35:08
Comentarios

¿Cómo paginar estos posts después de obtenerlos?

Andreas Hunter Andreas Hunter
28 sept 2018 14:11:03
3

Deberías usar get_terms() para taxonomías cuando sea posible.

<?php 
/* Añade tu taxonomía. */
$taxonomies = array( 
    'fabric_building_types',
);

$args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => true, 
    'exclude'           => array(), 
    'exclude_tree'      => array(), 
    'include'           => array(),
    'number'            => '', 
    'fields'            => 'all', 
    'slug'              => '', 
    'parent'            => '',
    'hierarchical'      => true, 
    'child_of'          => 0, 
    'get'               => '', 
    'name__like'        => '',
    'description__like' => '',
    'pad_counts'        => false, 
    'offset'            => '', 
    'search'            => '', 
    'cache_domain'      => 'core'
); 

$terms = get_terms( $taxonomies, $args );
foreach ( $terms as $term ) {

// aquí está mi código para obtener los posts del tipo de contenido personalizado

$posts_array = get_posts(
                        array( 'showposts' => -1,
                            'post_type' => 'fabric_building',
                            'tax_query' => array(
                                array(
                                'taxonomy' => 'fabric_building_types',
                                'field' => 'term_id',
                                'terms' => $term->term_id,
                                )
                            )
                        )
                    );
    print_r( $posts_array ); 
}
?>

Enlace al Codex: http://codex.wordpress.org/Function_Reference/get_terms

16 oct 2014 20:16:17
Comentarios

Gracias... pero el mismo problema persiste..array vacío.. ¿será porque estoy haciendo algo mal al definir el tipo de publicación personalizada ya que obtengo el array de términos..?

Parth Kumar Parth Kumar
16 oct 2014 20:37:40

¿Has probado el código actualizado de arriba?

Courtney Ivey Courtney Ivey
16 oct 2014 20:48:45

En realidad como sugirió Milo, el campo acepta un string..ese era el error..que ya resolví..

Parth Kumar Parth Kumar
16 oct 2014 20:50:54
0
global $post; $id = $post->ID;

$cat = get_the_category($id);
$loc = get_the_terms($id, 'taxonomy');
$posts = get_posts(
array('post_type' => 'post',
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'category' => $cat[0]->term_id, 
    'tax_query' => array(
        array(
            'taxonomy' => 'location',
            'field' => 'id',
            'terms' => $loc[0]->term_id,
        )
    )
)
);
print_r($posts);

esto debería funcionar.

14 sept 2016 05:59:20
0

Obtener publicaciones asignadas a la taxonomía actual

Puedes agregar el siguiente código en taxonomy-your_tax.php

<div class="a-article-wrapper">

                <?php 

                    $terms = wp_get_post_terms( $post->ID, 'your-taxonomy'); 
                    $terms_ids = [];

                    foreach ( $terms as $term ) {
                        $terms_ids[] = $term->term_id;
                    }

                    $args = array(
                        'post_type' => 'your-post-type',
                        'tax_query' => array(
                            'relation' => 'AND',
                            array(
                                'taxonomy' => 'your-taxonomy',
                                'field'    => 'term_id',
                                'terms'    => $terms_ids
                            )
                        ),
                    );

                    $query = new WP_Query($args);
                    if ( $query->have_posts() ) {
                        while ( $query->have_posts() ) {
                    ?>

                     <div class="row">
                        <div class="col-md-8 a-article-row">
                            <div class="row">
                                <?php $query->the_post();?>
                                <div class="a-post-time">
                                    <span class="a-current-date"><?php the_time('j F, D') ?></span>
                                    <span class="a-current-time"><?php the_time('g:i a') ?></span>
                                </div>
                                <div class="a-article-title">
                                    <?php the_title(); ?> 
                                </div>
                                <div class="a-article-content">
                                    <div id="excerpt"><?php the_excerpt(); ?></div>

                                    <?php the_content(); ?>
                                </div>
                                <div class="a-article-tags">
                                    <?php echo get_the_term_list( get_the_ID(), 'your-taxonomy', '', ',' ); ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <?php } } ?>
            </div> 
27 jun 2018 13:38:40