WP_Query por Nombre de Categoría

11 sept 2014, 18:55:32
Vistas: 66.2K
Votos: 4

Hola, estoy usando código para mostrar posts en un slider en una página, y en la parte superior tengo código para filtrarlo usando WP_Query. Aquí está, no estoy seguro de qué está mal:

<?php
/**
 * Contenido Destacado 2
 *
 */

$args = array(
    'post_type' => 'iv_post_type',
    'category'    => 'commitment',
);
$loop = new WP_Query( $args );      
if ( $loop->have_posts() ) : ?>

    <div id="video-sidebar-right">

        <div class="featured-current-wrapper">

            <div id="featured-content">     

                <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                        <div class="featured-post">
                            <?php if(has_post_thumbnail()) :?>
                                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'bcc-video-medium' ); ?></a>
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                <?php the_excerpt(); ?> 
                            <?php else :?>

                            <?php endif;?>

                        </div> <!-- .featured-post -->

                        <div class="featured-post-pager">
                            <span id="slider-prev" class="slider-nav">&larr; Siguiente Foto</span>
                            <span id="slider-next" class="slider-nav">&rarr; Foto Anterior</span>
                        </div>

                <?php endwhile; ?>


            </div><!-- #featured-content -->
        </div><!-- .featured-current-wrapper -->
    </div><!-- #video-sidebar-right -->

    <div id="video-featured-content">

        <div class="featured-video">
            <div class="videoWrapper">
                <iframe width="640" height="360" src="//www.youtube.com/embed/so_PO0dP3oA" frameborder="0" allowfullscreen></iframe>
            </div>
        </div>

        <?php wp_nav_menu( array( 'theme_location' => 'bcc-video-menu' ) ); ?>

        <p class="video-choose-cat">Elige una categoría de arriba para ver nuestros diferentes artículos.</p>

        <div id="slider-nav">

            <div id="slide-thumbs">

                <?php $slidecount = 1;

                while ( $loop->have_posts() ) : $loop->the_post(); ?>

                    <div class="slide-thumbs-single <?php echo ( $slidecount == -1 ) ? 'last' : ''; ?>">
                        <?php if(has_post_thumbnail()) :?>
                            <?php the_post_thumbnail( 'bcc-video-thumb' ); ?>
                        <?php else :?>

                        <?php endif;?>
                    </div>

                    <?php $slidecount++; ?>

                <?php endwhile; ?>

            </div><!-- .slide-thumbs -->
        </div><!-- #slider-nav -->
    </div><!-- #video-featured-content -->


<?php endif; ?>
3
Comentarios

Bienvenido/a a [wordpress.se]. ¿Cuál es tu pregunta?

birgire birgire
11 sept 2014 18:59:28

¡Hola! Todos los posts de ese tipo de contenido se muestran en la página. Solo quiero que se muestren los de una categoría específica. ¿Es posible? No estoy seguro de qué pasa con mis $args... incluso si cambio category por category_name no muestra nada en absoluto...

user6311172 user6311172
11 sept 2014 19:04:24

Por favor actualiza tu pregunta con esa información.

birgire birgire
11 sept 2014 19:25:44
Todas las respuestas a la pregunta 2
1
13

category es un parámetro válido para get_posts, pero no para WP_Query. Estos son los parámetros válidos para categorías en WP_Query:

  • cat (int) - utiliza el ID de la categoría.
  • category_name (string) - utiliza el slug de la categoría (NO el nombre).
  • category__and (array) - utiliza el ID de la categoría.
  • category__in (array) - utiliza el ID de la categoría.
  • category__not_in (array) - utiliza el ID de la categoría.

Puedes usar cat o category_name

Si no estás seguro de algún parámetro, simplemente haz un var_dump() de tu variable new WP_Query. En tu caso, haz lo siguiente:

?><pre><?php var_dump($loop); ?></pre><?php

Esto mostrará algo como esto:

object(WP_Query)#197 (49) {
  ["query"]=>
  array(2) {
    ["page"]=>
    string(0) ""
    ["pagename"]=>
    string(7) "profile"
  }
  ["query_vars"]=>
  array(62) {
    ["page"]=>
    int(0)
    ["pagename"]=>
    string(7) "profile"
    ["error"]=>
    string(0) ""
    ["m"]=>
    string(0) ""
    ["p"]=>
    int(0)
    ["post_parent"]=>
    string(0) ""
    ["subpost"]=>
    string(0) ""
    ["subpost_id"]=>
    string(0) ""
    ["attachment"]=>
    string(0) ""
    ["attachment_id"]=>
    int(0)
    ["name"]=>
    string(7) "profile"
    ["static"]=>
    string(0) ""
    ["page_id"]=>
    int(0)
    ["second"]=>
    string(0) ""
    ["minute"]=>
    string(0) ""
    ["hour"]=>
    string(0) ""
    ["day"]=>
    int(0)
    ["monthnum"]=>
    int(0)
    ["year"]=>
    int(0)
    ["w"]=>
    int(0)
    ["category_name"]=>
    string(0) ""
    ["tag"]=>
    string(0) ""
    ["cat"]=>
    string(0) ""
    ["tag_id"]=>
    string(0) ""
    ["author"]=>
    string(0) ""
    ["author_name"]=>
    string(0) ""
    ["feed"]=>
    string(0) ""
    ["tb"]=>
    string(0) ""
    ["paged"]=>
    int(0)
    ["comments_popup"]=>
    string(0) ""
    ["meta_key"]=>
    string(0) ""
    ["meta_value"]=>
    string(0) ""
    ["preview"]=>
    string(0) ""
    ["s"]=>
    string(0) ""
    ["sentence"]=>
    string(0) ""
    ["fields"]=>
    string(0) ""
    ["menu_order"]=>
    string(0) ""
    ["category__in"]=>
    array(0) {
    }
    ["category__not_in"]=>
    array(0) {
    }
    ["category__and"]=>
    array(0) {
    }
    ["post__in"]=>
    array(0) {
    }
    ["post__not_in"]=>
    array(0) {
    }
    ["tag__in"]=>
    array(0) {
    }
    ["tag__not_in"]=>
    array(0) {
    }
    ["tag__and"]=>
    array(0) {
    }
    ["tag_slug__in"]=>
    array(0) {
    }
    ["tag_slug__and"]=>
    array(0) {
    }
    ["post_parent__in"]=>
    array(0) {
    }
    ["post_parent__not_in"]=>
    array(0) {
    }
    ["author__in"]=>
    array(0) {
    }
    ["author__not_in"]=>
    array(0) {
    }
    ["ignore_sticky_posts"]=>
    bool(false)
    ["suppress_filters"]=>
    bool(false)
    ["cache_results"]=>
    bool(true)
    ["update_post_term_cache"]=>
    bool(true)
    ["update_post_meta_cache"]=>
    bool(true)
    ["post_type"]=>
    string(0) ""
    ["posts_per_page"]=>
    int(6)
    ["nopaging"]=>
    bool(false)
    ["comments_per_page"]=>
    string(2) "20"
    ["no_found_rows"]=>
    bool(false)
    ["order"]=>
    string(4) "DESC"
  }
  ["tax_query"]=>
  NULL
  ["meta_query"]=>
  object(WP_Meta_Query)#211 (2) {
    ["queries"]=>
    array(0) {
    }
    ["relation"]=>
    NULL
  }
  ["date_query"]=>
  bool(false)
  ["queried_object"]=>
  object(WP_Post)#236 (24) {
    ["ID"]=>
    int(265)
    ["post_author"]=>
    string(1) "1"
    ["post_date"]=>
    string(19) "2014-04-21 14:48:59"
    ["post_date_gmt"]=>
    string(19) "2014-04-21 12:48:59"
    ["post_content"]=>
    string(0) ""
    ["post_title"]=>
    string(4) "Test"
    ["post_excerpt"]=>
    string(0) ""
    ["post_status"]=>
    string(7) "publish"
    ["comment_status"]=>
    string(4) "open"
    ["ping_status"]=>
    string(4) "open"
    ["post_password"]=>
    string(0) ""
    ["post_name"]=>
    string(7) "profile"
    ["to_ping"]=>
    string(0) ""
    ["pinged"]=>
    string(0) ""
    ["post_modified"]=>
    string(19) "2014-09-10 19:54:58"
    ["post_modified_gmt"]=>
    string(19) "2014-09-10 17:54:58"
    ["post_content_filtered"]=>
    string(0) ""
    ["post_parent"]=>
    int(0)
    ["guid"]=>
    string(39) "http://localhost/wordpress/?page_id=265"
    ["menu_order"]=>
    int(0)
    ["post_type"]=>
    string(4) "page"
    ["post_mime_type"]=>
    string(0) ""
    ["comment_count"]=>
    string(1) "0"
    ["filter"]=>
    string(3) "raw"
  }
  ["queried_object_id"]=>
  int(265)
  ["request"]=>
  string(138) "SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND (wp_posts.ID = '265') AND wp_posts.post_type = 'page'  ORDER BY wp_posts.post_date DESC "
  ["posts"]=>
  &array(1) {
    [0]=>
    object(WP_Post)#212 (24) {
      ["ID"]=>
      int(265)
      ["post_author"]=>
      string(1) "1"
      ["post_date"]=>
      string(19) "2014-04-21 14:48:59"
      ["post_date_gmt"]=>
      string(19) "2014-04-21 12:48:59"
      ["post_content"]=>
      string(0) ""
      ["post_title"]=>
      string(4) "Test"
      ["post_excerpt"]=>
      string(0) ""
      ["post_status"]=>
      string(7) "publish"
      ["comment_status"]=>
      string(4) "open"
      ["ping_status"]=>
      string(4) "open"
      ["post_password"]=>
      string(0) ""
      ["post_name"]=>
      string(7) "profile"
      ["to_ping"]=>
      string(0) ""
      ["pinged"]=>
      string(0) ""
      ["post_modified"]=>
      string(19) "2014-09-10 19:54:58"
      ["post_modified_gmt"]=>
      string(19) "2014-09-10 17:54:58"
      ["post_content_filtered"]=>
      string(0) ""
      ["post_parent"]=>
      int(0)
      ["guid"]=>
      string(39) "http://localhost/wordpress/?page_id=265"
      ["menu_order"]=>
      int(0)
      ["post_type"]=>
      string(4) "page"
      ["post_mime_type"]=>
      string(0) ""
      ["comment_count"]=>
      string(1) "0"
      ["filter"]=>
      string(3) "raw"
    }
  }
  ["post_count"]=>
  int(1)
  ["current_post"]=>
  int(-1)
  ["in_the_loop"]=>
  bool(false)
  ["post"]=>
  object(WP_Post)#212 (24) {
    ["ID"]=>
    int(265)
    ["post_author"]=>
    string(1) "1"
    ["post_date"]=>
    string(19) "2014-04-21 14:48:59"
    ["post_date_gmt"]=>
    string(19) "2014-04-21 12:48:59"
    ["post_content"]=>
    string(0) ""
    ["post_title"]=>
    string(4) "Test"
    ["post_excerpt"]=>
    string(0) ""
    ["post_status"]=>
    string(7) "publish"
    ["comment_status"]=>
    string(4) "open"
    ["ping_status"]=>
    string(4) "open"
    ["post_password"]=>
    string(0) ""
    ["post_name"]=>
    string(7) "profile"
    ["to_ping"]=>
    string(0) ""
    ["pinged"]=>
    string(0) ""
    ["post_modified"]=>
    string(19) "2014-09-10 19:54:58"
    ["post_modified_gmt"]=>
    string(19) "2014-09-10 17:54:58"
    ["post_content_filtered"]=>
    string(0) ""
    ["post_parent"]=>
    int(0)
    ["guid"]=>
    string(39) "http://localhost/wordpress/?page_id=265"
    ["menu_order"]=>
    int(0)
    ["post_type"]=>
    string(4) "page"
    ["post_mime_type"]=>
    string(0) ""
    ["comment_count"]=>
    string(1) "0"
    ["filter"]=>
    string(3) "raw"
  }
  ["comment_count"]=>
  int(0)
  ["current_comment"]=>
  int(-1)
  ["found_posts"]=>
  int(1)
  ["max_num_pages"]=>
  int(0)
  ["max_num_comment_pages"]=>
  int(0)
  ["is_single"]=>
  bool(false)
  ["is_preview"]=>
  bool(false)
  ["is_page"]=>
  bool(true)
  ["is_archive"]=>
  bool(false)
  ["is_date"]=>
  bool(false)
  ["is_year"]=>
  bool(false)
  ["is_month"]=>
  bool(false)
  ["is_day"]=>
  bool(false)
  ["is_time"]=>
  bool(false)
  ["is_author"]=>
  bool(false)
  ["is_category"]=>
  bool(false)
  ["is_tag"]=>
  bool(false)
  ["is_tax"]=>
  bool(false)
  ["is_search"]=>
  bool(false)
  ["is_feed"]=>
  bool(false)
  ["is_comment_feed"]=>
  bool(false)
  ["is_trackback"]=>
  bool(false)
  ["is_home"]=>
  bool(false)
  ["is_404"]=>
  bool(false)
  ["is_comments_popup"]=>
  bool(false)
  ["is_paged"]=>
  bool(false)
  ["is_admin"]=>
  bool(false)
  ["is_attachment"]=>
  bool(false)
  ["is_singular"]=>
  bool(true)
  ["is_robots"]=>
  bool(false)
  ["is_posts_page"]=>
  bool(false)
  ["is_post_type_archive"]=>
  bool(false)
  ["query_vars_hash":"WP_Query":private]=>
  string(32) "7d1c96f43296ed3cb0cd8c4716b9b916"
  ["query_vars_changed":"WP_Query":private]=>
  bool(false)
  ["thumbnails_cached"]=>
  bool(false)
  ["stopwords":"WP_Query":private]=>
  NULL
}

EDITADO

Otra cosa para asegurarte, ¿estás utilizando taxonomías personalizadas o categorías integradas? Es muy importante conocer la diferencia ya que las taxonomías personalizadas no utilizan los parámetros de categoría, sino tax_query. Si no estás seguro, por favor lee mi respuesta sobre este tema aquí

Si estás usando una taxonomía personalizada, entonces deberías eliminar el parámetro category y reemplazarlo con tax_query

Ejemplo:

'tax_query' => array(
        array(
            'taxonomy' => 'NOMBRE DE TU TAXONOMÍA',
            'field'    => 'slug',
            'terms'    => 'SLUG DE TU TÉRMINO',
        ),
    ),
11 sept 2014 19:16:36
Comentarios

supongamos que tienes el nombre de la categoría y no el ID de la categoría, ¿cómo obtienes el ID de la categoría a partir del nombre?

Karue Benson Karue Karue Benson Karue
2 dic 2021 05:16:09
0

prueba este

$anArray = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'TU TAXONOMÍA PERSONALIZADA',
            'terms'    => 'ID DE CATEGORÍA',
        ),
    ),
);

puedes encontrar los valores en wp_term_taxonomy desde tu base de datos.

10 feb 2022 17:12:46