我正在尝试使用称为“事件”的自定义分类法来过滤自定义帖子。我拥有的代码应检查当前帖子的“事件”分类法的name
,然后仅列出同一组内的帖子。
我已经检查了法典页面https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters,但无法使其正常工作。有人有什么建议吗?
$post_terms = wp_get_object_terms($post->ID, 'events');
$post_term = $post_terms[0]->name;
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__not_in' => array(124470, 124455),
'tax_query' => array(
array(
'taxonomy' => 'events',
'field' => 'name',
'terms' => $post_term,
),
),
);
更新,根据要求在下面添加了代码。
/* custom taxonomies */
function my_custom_events_taxonomies(){
register_taxonomy(
'events',
'events',
array(
'label' => 'Events type',
'rewrite' => array( 'slug' => 'Events'),
'hierarchical' => false,
'custom-fields' => true,
)
);
}
add_action ( 'init', 'my_custom_events_taxonomies');
下面的自定义帖子类型:
function events_post_type() {
$labels = array(
'name' => _x( 'Events', 'Post Type General Name', 'remco' ),
'singular_name' => _x( 'Events', 'Post Type Singular Name', 'remco' ),
'menu_name' => __( 'Events', 'remco' ),
'name_admin_bar' => __( 'Events', 'remco' ),
'archives' => __( 'Events Archives', 'remco' ),
'parent_item_colon' => __( 'Parent Events:', 'remco' ),
'all_items' => __( 'All Events', 'remco' ),
'add_new_item' => __( 'Add New Events', 'remco' ),
'add_new' => __( 'Add New', 'remco' ),
'new_item' => __( 'New Item', 'remco' ),
'edit_item' => __( 'Edit Item', 'remco' ),
'update_item' => __( 'Update Item', 'remco' ),
'view_item' => __( 'View Item', 'remco' ),
'search_items' => __( 'Search Item', 'remco' ),
'not_found' => __( 'Not found', 'remco' ),
'not_found_in_trash' => __( 'Not found in Trash', 'remco' ),
'insert_into_item' => __( 'Insert into item', 'remco' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'remco' ),
'items_list' => __( 'Items list', 'remco' ),
'items_list_navigation' => __( 'Items list navigation', 'remco' ),
'filter_items_list' => __( 'Filter items list', 'remco' ),
);
$args = array(
'label' => __( 'Events', 'remco' ),
'description' => __( 'Events', 'remco' ),
'labels' => $labels,
'supports' => array( 'title', 'editor','revisions', 'thumbnail', 'custom-fields' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 9,
'menu_icon' => 'dashicons-calendar' ,
'show_in_admin_bar' => true,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'taxonomies' => 'events'
);
register_post_type( 'Events', $args );
}
add_action( 'init', 'events_post_type', 0 );
答案 0 :(得分:0)
使用自定义表情符号从自定义帖子类型获取帖子的标准方法是
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);