¿Cómo puedo consultar un tipo de publicación personalizada con una taxonomía personalizada?

21

Por alguna razón, me resulta difícil obtener publicaciones usando una taxonomía personalizada ... ¿alguien puede desentrañar mi estupidez?

 $args = array(
    'post_type' => 'adverts',
    'advert_tag' => 'politics' // Doesn't seem to work.
  );

query_posts($args); 

while ( have_posts() ) : the_post();
 //Show Posts
endwhile;

Declaración de taxonomía:

add_action( 'init', 'add_custom_taxonomy', 0 );
function add_custom_taxonomy() {
register_taxonomy('advert_tag', 'Adverts', array(
  'hierarchical' => true,
  'labels' => array(
    'name' => _x( 'Advert Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'Advert Tag', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Advert Tags' ),
    'all_items' => __( 'All Advert Tags' ),
    'parent_item' => __( 'Parent Advert Tag' ),
    'parent_item_colon' => __( 'Parent Advert Tag:' ),
    'edit_item' => __( 'Edit Advert Tag' ),
    'update_item' => __( 'Update Advert Tag' ),
    'add_new_item' => __( 'Add New Advert Tag' ),
    'new_item_name' => __( 'New Advert Tag Name' ),
    'menu_name' => __( 'Advert Tags' ),
  ),
  'rewrite' => array(
    'slug' => 'advert-tags',
    'with_front' => false,
    'hierarchical' => true
  ),
));
  }

Declaración de tipo de publicación personalizada:

  add_action( 'init', 'create_post_type' );
  function create_post_type() {
    register_post_type( 'Adverts',
    array(
        'labels' => array(
            'name' => __( 'Adverts' ),
            'singular_name' => __( 'Advert'),
            'add_new' => __( 'Add New' ),
            'add_new_item' => __( 'Add a New Advert' ),
            'edit' => __( 'Edit' ),
            'edit_item' => __( 'Edit Advert' ),
            'new_item' => __( 'New Advert' ),
            'view' => __( 'View' ),
            'view_item' => __( 'View Advert' ),
            'search_items' => __( 'Search Adverts' ),
            'not_found' => __( 'No Adverts found' ),
            'not_found_in_trash' => __( 'No Adverts found in Trash' ),
            ),
        'supports' => array(
                'title',
                'thumbnail',
            ),
        'has_archive' => true,
        'menu_position' => 10,
        'public' => true,
        'rewrite' => array( 'slug' => 'adverts' ),
        'taxonomies' => array('advert_tag')
    )
);

}

    
pregunta Stephen 07.02.2013 - 14:01

3 respuestas

29

Los primeros en no usen query_posts() nunca , lea más sobre esto aquí: ¿Cuándo debería usar WP_Query vs query_posts () vs get_posts ()? .

Tienes que usar WP_Query para obtener las publicaciones que necesites. Lea la documentación para ello. En su caso, la consulta podría ser así:

$the_query = new WP_Query( array(
    'post_type' => 'Adverts',
    'tax_query' => array(
        array (
            'taxonomy' => 'advert_tag',
            'field' => 'slug',
            'terms' => 'politics',
        )
    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    // Show Posts ...
endwhile;

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren't stomping on the 
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
    
respondido por el Eugene Manuilov 07.02.2013 - 14:11
12

estoy utilizando esta consulta para obtener publicaciones personalizadas (Preguntas frecuentes sobre publicaciones) con su taxonomía personalizada (faq_category). ya que el parámetro {taxonomy} en WP_Query args está en desuso desde v.3.1 e introdujo {tax_query}. A continuación se muestra el código que funciona perfectamente.

$query = new WP_Query( array(
    'post_type' => 'faqs',          // name of post type.
    'tax_query' => array(
        array(
            'taxonomy' => 'faq_category',   // taxonomy name
            'field' => 'term_id',           // term_id, slug or name
            'terms' => 48,                  // term id, term slug or term name
        )
    )
) );

while ( $query->have_posts() ) : $query->the_post();
    // do stuff here....
endwhile;

/**
 * reset the orignal query
 * we should use this to reset wp_query
 */
wp_reset_query();
    
respondido por el Aamer Shahzad 19.11.2015 - 22:04
0

Esta respuesta ya no es válida ya que wordpress cambia la información de sus parámetros de taxonomía. por favor use de esta manera Funcionará. Esto funciona para mi. "tax_query" reemplaza con "tax". Espero que funcione.

$the_query = new WP_Query( array(
    'post_type' => 'Adverts',
    'tax' => array(
        array (
            'taxonomy' => 'advert_tag',
            'field' => 'slug',
            'terms' => 'politics',
        )
    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    // Show Posts ...
endwhile;

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren't stomping on the 
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
    
respondido por el mamunuzaman 26.09.2018 - 16:09

Lea otras preguntas en las etiquetas