自定义帖子类型的WP_QUERY查询的问题

时间:2016-11-17 作者:DCdesign

下午好,我的CPT查询有以下问题,我想告诉我帖子的类型、分类法“课程”和最后3篇帖子。在分类法完成之前,查询是正确的,它会显示所有分类法(课程)的帖子。

查询:

<?php 

$the_query = new WP_Query( array(
    \'post_type\'         =>  \'circulares\',
    \'posts_per_page\'    =>  2,
    \'tax_query\'         =>  array(
        \'taxonomy\'      =>  \'curso\',
        \'field\'         =>  \'slug\',
        \'terms\'         =>  \'tercero\',
    ),
));


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

<?php the_title(); ?><br>

<?php endwhile;


wp_reset_postdata(); ?>
分类法:

    add_action( \'init\', \'create_circular_taxonomies\', 0 );


function create_circular_taxonomies() {

        $labels = array(
        \'name\' => _x( \'Cursos\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Curso\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Buscar por Curso\' ),
        \'all_items\' => __( \'Todos los Cursos\' ),
        \'parent_item\' => __( \'Curso padre\' ),
        \'parent_item_colon\' => __( \'Curso padre:\' ),
        \'edit_item\' => __( \'Editar Curso\' ),
        \'update_item\' => __( \'Actualizar Curso\' ),
        \'add_new_item\' => __( \'Añadir nuevo Curso\' ),
        \'new_item_name\' => __( \'Nombre del nuevo Curso\' ),
);

register_taxonomy( \'curso\', array( \'circulares\' ), array(
        \'hierarchical\' => true,
        \'labels\' => $labels, 
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'cursos\' ),
));

}
注册帖子类型自定义:

add_action( \'init\', \'circulares\');

function circulares() {
    $labels = array(
        \'name\'              => _x( \'Circulares\', \'post type general name\' ),
        \'singular_name\'     => _x( \'Circular\', \'post type singular name\' ),
        \'add_new\'           => _x( \'Añadir nueva\', \'circular\' ),
        \'add_new_item\'      => __( \'Añadir nueva circular\' ),
        \'edit_item\'         => __( \'Editar circular\' ),
        \'new_item\'          => __( \'Nueva circular\' ),
        \'view_item\'         => __( \'Ver circular\' ),
        \'search_item\'       => __( \'Buscar circulares\' ),
        \'not_found\'         => __( \'No se han encontrado circulares\' ),
        \'not_found_in_trash\'=> __( \'No se ha encontrado circulares en la papelera\' ),
        \'parent_item_colon\' => \'\'                   
    );



    $args = array( 
        \'labels\'                => $labels,
        \'public\'                => true,
        \'publicly_queryable\'    => true,
        \'show_ui\'               => true,
        \'query_var\'             => true,
        \'rewrite\'               => true,
        \'capability_type\'       => \'post\',
        \'hierarchical\'          => false,
        \'menu_position\'         => null,
        \'supports\'              => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
    );  



    register_post_type( \'Circulares\', $args );
}

1 个回复
最合适的回答,由SO网友:czerspalace 整理而成

Tax\\u查询应该是一个数组数组。现在,它是一个一维数组。您可以尝试:

$the_query = new WP_Query( array(
    \'post_type\'         =>  \'circulares\',
    \'posts_per_page\'    =>  2,
    \'tax_query\'         =>  array(
                              array(
                                \'taxonomy\'      =>  \'curso\',
                                \'field\'         =>  \'slug\',
                                \'terms\'         =>  \'tercero\',
                             ))
));