admin管理员组

文章数量:1328488

I have some faqs on my site and want to show only faqs under a certain category. The categories include worker, company, test.

Here is my code:

$faq = new WP_Query(array(
          'post_type'=>'faq',
          'order' => 'DESC',
          'category_name' => 'test',
        ));
  while($faq->have_posts()) : $faq->the_post();

this should be showing me all the posts under the category of test but it is not doing that. I have also tried cat => (id) and still no result. The loop is always empty.

Here are the registration functions:

function faq_post_type() {

// these are the labels in the admin interface, edit them as you like
$labels = array(
    'name'                => _x( 'FAQs', 'Post Type General Name', 'faq' ),
    'singular_name'       => _x( 'FAQ', 'Post Type Singular Name', 'faq' ),
    'menu_name'           => __( 'FAQ', 'faq' ),
    'parent_item_colon'   => __( 'Parent Item:', 'faq' ),
    'all_items'           => __( 'All Items', 'faq' ),
    'view_item'           => __( 'View Item', 'faq' ),
    'add_new_item'        => __( 'Add New FAQ Item', 'faq' ),
    'add_new'             => __( 'Add New', 'faq' ),
    'edit_item'           => __( 'Edit Item', 'faq' ),
    'update_item'         => __( 'Update Item', 'faq' ),
    'search_items'        => __( 'Search Item', 'faq' ),
    'not_found'           => __( 'Not found', 'faq' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'faq' ),
);
$args = array(
    // use the labels above
    'labels'              => $labels,
    // we'll only need the title, the Visual editor and the excerpt fields for our post type
    'supports'            => array( 'title', 'editor', 'excerpt', ),
    // we're going to create this taxonomy in the next section, but we need to link our post type to it now
    'taxonomies'          => array( 'faq_tax' ),
    // make it public so we can see it in the admin panel and show it in the front-end
    'public'              => true,
    // show the menu item under the Pages item
    'menu_position'       => 20,
    // show archives, if you don't need the shortcode
    'has_archive'         => true,
);
register_post_type( 'faq', $args );

}
// hook into the 'init' action
add_action( 'init', 'faq_post_type', 0 );

and category:

function faq_post_category() {

// again, labels for the admin panel
$labels = array(
    'name'                       => _x( 'FAQ Categories', 'Taxonomy General Name', 'faq' ),
    'singular_name'              => _x( 'FAQ Category', 'Taxonomy Singular Name', 'faq' ),
    'menu_name'                  => __( 'FAQ Categories', 'faq' ),
    'all_items'                  => __( 'All FAQ Cats', 'faq' ),
    'parent_item'                => __( 'Parent FAQ Cat', 'faq' ),
    'parent_item_colon'          => __( 'Parent FAQ Cat:', 'faq' ),
    'new_item_name'              => __( 'New FAQ Cat', 'faq' ),
    'add_new_item'               => __( 'Add New FAQ Cat', 'faq' ),
    'edit_item'                  => __( 'Edit FAQ Cat', 'faq' ),
    'update_item'                => __( 'Update FAQ Cat', 'faq' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'faq' ),
    'search_items'               => __( 'Search Items', 'faq' ),
    'add_or_remove_items'        => __( 'Add or remove items', 'faq' ),
    'choose_from_most_used'      => __( 'Choose from the most used items', 'faq' ),
    'not_found'                  => __( 'Not Found', 'faq' ),
);
$args = array(
    // use the labels above
    'labels'                     => $labels,
    // taxonomy should be hierarchial so we can display it like a category section
    'hierarchical'               => true,
    // again, make the taxonomy public (like the post type)
    'public'                     => true,
);
// the contents of the array below specifies which post types should the taxonomy be linked to
register_taxonomy( 'faq_category', array( 'faq' ), $args );

}

// hook into the 'init' action
add_action( 'init', 'faq_post_category', 0 );

I have some faqs on my site and want to show only faqs under a certain category. The categories include worker, company, test.

Here is my code:

$faq = new WP_Query(array(
          'post_type'=>'faq',
          'order' => 'DESC',
          'category_name' => 'test',
        ));
  while($faq->have_posts()) : $faq->the_post();

this should be showing me all the posts under the category of test but it is not doing that. I have also tried cat => (id) and still no result. The loop is always empty.

Here are the registration functions:

function faq_post_type() {

// these are the labels in the admin interface, edit them as you like
$labels = array(
    'name'                => _x( 'FAQs', 'Post Type General Name', 'faq' ),
    'singular_name'       => _x( 'FAQ', 'Post Type Singular Name', 'faq' ),
    'menu_name'           => __( 'FAQ', 'faq' ),
    'parent_item_colon'   => __( 'Parent Item:', 'faq' ),
    'all_items'           => __( 'All Items', 'faq' ),
    'view_item'           => __( 'View Item', 'faq' ),
    'add_new_item'        => __( 'Add New FAQ Item', 'faq' ),
    'add_new'             => __( 'Add New', 'faq' ),
    'edit_item'           => __( 'Edit Item', 'faq' ),
    'update_item'         => __( 'Update Item', 'faq' ),
    'search_items'        => __( 'Search Item', 'faq' ),
    'not_found'           => __( 'Not found', 'faq' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'faq' ),
);
$args = array(
    // use the labels above
    'labels'              => $labels,
    // we'll only need the title, the Visual editor and the excerpt fields for our post type
    'supports'            => array( 'title', 'editor', 'excerpt', ),
    // we're going to create this taxonomy in the next section, but we need to link our post type to it now
    'taxonomies'          => array( 'faq_tax' ),
    // make it public so we can see it in the admin panel and show it in the front-end
    'public'              => true,
    // show the menu item under the Pages item
    'menu_position'       => 20,
    // show archives, if you don't need the shortcode
    'has_archive'         => true,
);
register_post_type( 'faq', $args );

}
// hook into the 'init' action
add_action( 'init', 'faq_post_type', 0 );

and category:

function faq_post_category() {

// again, labels for the admin panel
$labels = array(
    'name'                       => _x( 'FAQ Categories', 'Taxonomy General Name', 'faq' ),
    'singular_name'              => _x( 'FAQ Category', 'Taxonomy Singular Name', 'faq' ),
    'menu_name'                  => __( 'FAQ Categories', 'faq' ),
    'all_items'                  => __( 'All FAQ Cats', 'faq' ),
    'parent_item'                => __( 'Parent FAQ Cat', 'faq' ),
    'parent_item_colon'          => __( 'Parent FAQ Cat:', 'faq' ),
    'new_item_name'              => __( 'New FAQ Cat', 'faq' ),
    'add_new_item'               => __( 'Add New FAQ Cat', 'faq' ),
    'edit_item'                  => __( 'Edit FAQ Cat', 'faq' ),
    'update_item'                => __( 'Update FAQ Cat', 'faq' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'faq' ),
    'search_items'               => __( 'Search Items', 'faq' ),
    'add_or_remove_items'        => __( 'Add or remove items', 'faq' ),
    'choose_from_most_used'      => __( 'Choose from the most used items', 'faq' ),
    'not_found'                  => __( 'Not Found', 'faq' ),
);
$args = array(
    // use the labels above
    'labels'                     => $labels,
    // taxonomy should be hierarchial so we can display it like a category section
    'hierarchical'               => true,
    // again, make the taxonomy public (like the post type)
    'public'                     => true,
);
// the contents of the array below specifies which post types should the taxonomy be linked to
register_taxonomy( 'faq_category', array( 'faq' ), $args );

}

// hook into the 'init' action
add_action( 'init', 'faq_post_category', 0 );
Share Improve this question edited Sep 17, 2017 at 2:48 JohnSnow asked Sep 17, 2017 at 0:35 JohnSnowJohnSnow 1151 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Your query should look like this (more or less)

$args = array(
    'post_type' => 'FAQ ',
    'tax_query' => array(
        array(
            'taxonomy' => 'faq_category',
            'field'    => 'slug',
        ),
    ),
);
$query = new WP_Query( $args );

本文标签: wp queryshowing custom post types of a certain category only