Özel bir yazı tipini özel bir taksonomiyle nasıl sorgularım?


26

Bazı nedenlerden dolayı, özel bir taksonomi kullanarak herhangi bir yazıyı kapmak için bir mücadele buluyorum ... herkes aptallığımı çözebilir mi?

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

query_posts($args); 

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

Taksonomi Bildirimi:

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
  ),
));
  }

Özel Gönderi Türü Beyanı:

  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')
    )
);

}

Yanıtlar:


37

Firs kullanmayın query_posts()hiç : Bununla ilgili daha fazla okumak, ) siz get_posts vs query_posts () vs WP_Query ne zaman kullanmalıyım (? .

İhtiyacınız olan WP_Queryyayınları almak için kullanmanız gerekir. Bunun için belgeleri okuyun . Senin durumunda, sorgu şöyle olabilir:

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

2
Sadece tüm gönderileri 'İlanlar' özel gönderim tipiyle çektiğini farkettim. Ancak bu işi yapıyor gibi görünüyor: $ the_query = new WP_Query (array ('post_type' => 'Reklamlar', 'advert_tag' => 'siyaset'));
— Stephen,

@Stephen {tax}, 3.1 sürümünden beri {tax_query} ve {tax_query} lehine kaldırıldı. bu hala işe yarıyor ancak kullanımdan kaldırılmış işlevleri kullanmamalıyız. tax_query, bir dizi taksonomi sorgusuyla birlikte kullanılır. Sıkça Sorulan Sorular Özel Gönderi türü üzerinde çalışıyordum ve bu benim için çalıştı, WP_Query'deki {tax} taxonomy slug argümanı gibi.
— Aamer Shahzad

16

Özel sorguları (faq_category) ile özel yazılar (SSS Mesajları) almak için bu sorguyu kullanıyorum. WP_Query args parametresindeki {taxonomy} parametresi v.3.1'den beri kullanımdan kaldırıldı ve {tax_query} tanıtıldı. mükemmel çalışan kod aşağıdadır.

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

Bu doğru cevap - kabul edilen cevap tax_query bir dizi dizisi gerektirdiğinden taksonomiye göre filtreleme yapmaz. Bu iç içe geçmiş yöntem, bunun işe yaraması için önemlidir. Cevabınız için teşekkürler)
— Tom Dyer

evet haklısın, hoş geldiniz Tom Dyer
— Aamer Shahzad

Evet, bu da taksonomi şablonunun çalışmasını sağlamamda bana yardımcı oldu. Teşekkür ederim!
— user3135691

Hey @AamerShahzad Ben de aynı soruyu sordum ve cevabınızı kullandım ancak sayfa hiç yazı çekmiyor. Bana yardım edebilir misin? stackoverflow.com/questions/55783769/…
— Desi

-1

Bu cevap artık geçerli değil çünkü wordpress taksonomisi parametre bilgilerini değiştiriyor. lütfen bu şekilde kullan. Çalışacak. Benim için çalışıyor. "tax_query", "vergi" ile değiştirilir. işe yarayacağını umuyorum.

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

Tam tersi - taxeski yoldu, tax_querymevcut (v3.1 +) yoluydu.
— WebElaine

Peki ben v4.5 çalışıyorum ve benimle çalışır
— mamunuzaman

WP geriye uyumlu olmasıyla ünlüdür. Eski yol hala işe yarıyor, ancak kullanımdan kaldırıldı, bu yüzden sonunda kaldırılabilir ve daha yeni bir yöntem kullanmak daha güvenli olabilir.
— WebElaine
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.