ACF使用分类法查询自定义帖子类型 [英] ACF Querying custom post types with taxonomies

查看:110
本文介绍了ACF使用分类法查询自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以查询和显示所有自定义帖子类型,但是当尝试基于分类法查询它们时也将无法工作。有人请帮忙!

I can query and display all custom post types, but when trying to query them based on taxonomy as well it will not work. Somebody please help!

分类功能

add_action( 'init', 'create_project_type_taxonomies', 0 );

function create_project_type_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
    'name'              => _x( 'Project Types', 'taxonomy general name' ),
    'singular_name'     => _x( 'Project Type', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Project Types' ),
    'all_items'         => __( 'All Project Types' ),
    'parent_item'       => __( 'Parent Project Type' ),
    'parent_item_colon' => __( 'Parent Project Type:' ),
    'edit_item'         => __( 'Edit Project Type' ),
    'update_item'       => __( 'Update Project Type' ),
    'add_new_item'      => __( 'Add New Project Type' ),
    'new_item_name'     => __( 'New Project Type' ),
    'menu_name'         => __( 'Project Type' ),
);

$args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'project_type' ),
);

register_taxonomy( 'project_type', array( 'project' ), $args );
}

查询-无需分类行

<?php 
        $projects = new WP_Query(
            array(
                'post_type' => 'project',
                'project_type' => 'music_videos',
            )
        ); 
        ?>

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

                <!--some query stuff which works-->

                <?php endwhile; ?>
    <?php endif; wp_reset_query(); ?>


推荐答案

您必须使用tax_query参数,如所述此处

You have to use tax_query parameter, like described here

$args = array(
    'post_type' => 'project',
    'tax_query' => array(
        array(
            'taxonomy' => 'project_type',
            'field'    => 'slug',
            'terms'    => 'music_videos',
        ),
    ),
);
$projects = new WP_Query( $args );

希望它会有所帮助:)

这篇关于ACF使用分类法查询自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆