Php WordPress查询自定义帖子类型

Php WordPress查询自定义帖子类型,php,sql,wordpress,function,custom-post-type,Php,Sql,Wordpress,Function,Custom Post Type,我需要一些帮助,从自定义帖子类型中查询一些具有特定类别的帖子,因为它似乎不起作用。下面是myfunctions.php文件中的代码,它注册了事件的post类型&事件类别的自定义分类法,因此将其与通常的“post”类别分开 function cpt_events() { $labels = array( 'name' => 'Events', 'singular_name' => 'Event',

我需要一些帮助,从自定义帖子类型中查询一些具有特定类别的帖子,因为它似乎不起作用。下面是myfunctions.php文件中的代码,它注册了事件的post类型&事件类别的自定义分类法,因此将其与通常的“post”类别分开

function cpt_events() {
    $labels = array(
        'name'               => 'Events',
        'singular_name'      => 'Event',
        'menu_name'          => 'Events',
        'name_admin_bar'     => 'Event',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Event',
        'new_item'           => 'New Event',
        'edit_item'          => 'Edit Event',
        'view_item'          => 'View Event',
        'all_items'          => 'All Events',
        'search_items'       => 'Search Events',
        'parent_item_colon'  => 'Parent Event',
        'not_found'          => 'No Events Found',
        'not_found_in_trash' => 'No Events Found in Trash'
  );

  $args = array(
    'labels'              => $labels,
    'public'              => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'show_ui'             => true,
    'show_in_nav_menus'   => true,
    'show_in_menu'        => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-calendar-alt',
    'capability_type'     => 'post',
    'hierarchical'        => false,
    'supports'            => array( 'title', 'author', 'thumbnail' ),
    'has_archive'         => true,
    'rewrite'             => array( 'with_front' => false, 'slug' => 'events' ),
    'query_var'           => true
  );

  register_post_type( 'events', $args );
}
add_action( 'init', 'cpt_events' );

function events_tax() {
  $args = array(
    'hierarchical' => true
  );
  register_taxonomy( 'events_category', 'events', $args );
}
add_action( 'init', 'events_tax', 0 );
然后我还有我试图用来查询帖子的代码:

$events_args = array(
    'post_type' => 'events',
    'post_status' => 'publish',
    'category_name' => 'of-rifle-club1',
    'posts_per_page' => 3,
    'orderby' => 'date', 
    'order' => 'DESC'
);
$loop = new WP_Query($events_args);
我已将此参数用于WordPress中的默认帖子,但不幸的是,它不适用于上述自定义帖子类型和分类:

'category_name' => 'OF Rifle Club'
这基本上就是我要做的,只查询类别名称为“of Rifle Club”的帖子


任何帮助都将不胜感激。谢谢

使用
税务查询
。检查下面的代码

$events_args = array(
    'post_type'      => 'events',
    'post_status'    => 'publish',
    'posts_per_page' => 3,
    'orderby'        => 'date', 
    'order'          => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'events_category',
            'field'    => 'slug',
            'terms'    => 'of-rifle-club1',
        ),
    )
);

$loop = new WP_Query( $events_args );

非常感谢,我正在查看税务查询,但无法使其工作!这些参数中的每一个都意味着什么?分类法、字段和术语?再次感谢!欢迎你可以在这里查看更多信息