Site icon Hip-Hop Website Design and Development

Filtering custom post type query

Thanks to @the_dramatist for the following code in answer to this previous question.

I have a follow on, which is that I’d like to use the the following but break it down/filter it so I can pull in individual categories from the custom taxonomy.

For example, the follow outputs:

General

Rules

etc…

<?php
    $terms = get_terms('sections');
    foreach($terms as $term) {
        $posts = get_posts(
            array(
                'post_type' => 'questions', // Post type
                'tax_query' => array(
                    array(
                        'taxonomy' => 'sections', // Taxonomy name
                        'field' => 'slug',
                        'terms' => $term->slug
                    )
                ),
                'posts_per_page' => -1,
                'orderby'       => 'menu_order',
                'order'         => 'ASC'
            )
        );
    ?>
    <h3 class="text-center"><?php echo $term->name; ?> </h3>

    <ul class="accordion block multiple" data-accordion data-allow-all-closed="true">
        <?php
            foreach($posts as $post) { ?>
                <li class="accordion-item" data-accordion-item>
                    <!-- Accordion tab title -->
                    <a href="#" class="accordion-title"><?php echo $post->post_title; ?></a>

                    <!-- Accordion tab content: it would start in the open state due to using the `is-active` state class. -->
                    <div class="accordion-content" data-tab-content>
                        <?php echo $post->post_content; ?>
                    </div>
                </li>
        <?php } ?>
    </ul>

<?php } ?>

My Question now is:

If I only wanted to display one category, for example General, how would I filter this?
I’ve tried include and post_in followed by the ID of the General category but this doesn’t work.

Apologies if this is a simple question, but something about cpt and taxonomies just baffles me!


UPDATE -CANT GET THIS TO WORK?

Using @the_dramatist’s answer below I’m still getting all sections, can’t seem to single it out. I’ve tried with ID’s as below answer states and even the slug.

<?php
    $terms = get_terms('sections');
    foreach($terms as $term) {
        $posts = get_posts(
            array(
                'post_type' => 'questions', // Post type
                'tax_query' => array(
                    array(
                        'taxonomy' => 'sections', // Taxonomy name
                        'field' => 'slug',
                        'terms' => 'general'
                    )
                ),
                'posts_per_page' => -1,
                'orderby'       => 'menu_order',
                'order'         => 'ASC'
            )
        );
    ?>
    <h3 class="text-center"><?php echo $term->name; ?> </h3>

    <ul class="accordion block multiple" data-accordion data-allow-all-closed="true">
        <?php
            foreach($posts as $post) { ?>
                <li class="accordion-item" data-accordion-item>
                    <!-- Accordion tab title -->
                    <a href="#" class="accordion-title"><?php echo $post->post_title; ?></a>

                    <!-- Accordion tab content: it would start in the open state due to using the `is-active` state class. -->
                    <div class="accordion-content" data-tab-content>
                        <?php echo $post->post_content; ?>
                    </div>
                </li>
        <?php } ?>
    </ul>

<?php } ?>

or using this tax_query:

'tax_query' => array(
        array(
            'taxonomy' => 'sections', // Taxonomy name
            'field' => 'term_id',
            'terms' => '1005'
        )
    ),

Both times all taxonomies are being displayed and yet it all looks right.

UPDATE 2

This should all be working correctly, but it’s not. Could it be something to do with the way I’ve set up my custom post type and taxonomies? This is how I’ve got them:

    add_action( 'init', 'questions_post_type');
function questions_post_type() {
    $labels = array(
        'name'                  => 'Questions',                                 // This is the Title of the Group
        'singular_name'         => 'Question',                              // This is the individual type
        'add_new'               => 'Add New',                                   // The add new menu item
        'add_new_item'          => 'Add New Question',                      // Add New Display Title
        'edit'                  => 'Edit',                                      // Edit Dialog
        'edit_item'             => 'Edit Questions',                            // Edit Display Title
        'new_item'              => 'New Question',                          // New Display Title
        'view_item'             => 'View Question',                             // View Display Title
        'search_items'          => 'Search Questions',                          // Search Custom Type Title
        'not_found'             => 'No questions yet, why not create some?',    // This displays if there are no entries yet
        'not_found_in_trash'    => 'No questions found in Trash',               // This displays if there is nothing in the trash
        'parent_item_colon'     => ''
    );
    register_post_type( 'questions', array(
        'labels'                => $labels,
        'public'                => true,
        'publicly_queryable'    => true,
        'exclude_from_search'   => false,
        'show_ui'               => true,
        'menu_position'         => 20,                                          // this is what order you want it to appear in on the left hand side menu
        'menu_icon'             => 'dashicons-editor-help',                     // the icon for the custom post type menu. uses built-in dashicons (CSS class name)
        'query_var'             => true,
        'rewrite'               => array(
            'with_front' => false,
            'slug' => 'questions'                                                // you can specify its url slug
        ),
        'capability_type'       => 'page',
        'has_archive'           => true,
        'hierarchical'          => false,
        'supports'              => array( 'title', 'editor', 'revisions')
    ));

}

register_taxonomy( 'sections',
    array('questions'), // if you change the name of register_post_type( 'directions', then you have to change this
    array(
        'hierarchical'  => true,
        'labels'        => array(
            'name'          => __( 'Sections', 'jointswp' ),         // name of the custom taxonomy
            'singular_name' => __( 'Section', 'jointswp' ),          // single taxonomy name
            'menu_name'     => __( 'Sections', 'jointswp'),          // name as it appears in Dashboard menu
            'search_items'  => __( 'Search Sections', 'jointswp' ),  // search title for taxomony
            'all_items'     => __( 'All Sections', 'jointswp' ),     // all title for taxonomies
            'edit_item'     => __( 'Edit Section', 'jointswp' ),     // edit custom taxonomy title
            'update_item'   => __( 'Update Section', 'jointswp' ),   // update title for taxonomy
            'add_new_item'  => __( 'Add New Section', 'jointswp' ),  // add new title for taxonomy
            'new_item_name' => __( 'New Section', 'jointswp' )       // name title for taxonomy
        ),
        'show_admin_column' => true,
        'show_ui'       => true,
        'query_var'     => true,
        'rewrite'       => array(
            'with_front'    => false,
            'slug'          => 'sections'
        ),
        'hierarchical'          => true,
        'update_count_callback' => '_update_post_term_count'
    )
);