terms – Filtering custom post type query

[ad_1]

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

I’ve a observe on, which is that I’d like to make use of the the next but break it down/filter it so I can pull in particular person classes from the custom taxonomy.

For instance, the observe outputs:

General

  • Question 1
  • Answer 1
  • Question 2
  • Answer 2

Rules

  • Question 3
  • Answer 3
  • Question 4
  • Answer 4

etc…

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

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

                    <!-- Accordion tab content: it will begin within the open state due to utilizing 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 could be:

If I solely wished to display one category, for instance General, how would I filter this?
I’ve tried embrace and post_in adopted by the ID of the General category but this does not work.

Apologies if this can be a easy query, but one thing about cpt and taxonomies simply baffles me!


UPDATE -CANT GET THIS TO WORK?

Using @the_dramatist’s answer below I’m nonetheless getting all sections, can’t appear 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 $time period) {
        $posts = get_posts(
            array(
                'post_type' => 'questions', // Post type
                'tax_query' => array(
                    array(
                        'taxonomy' => 'sections', // Taxonomy title
                        'area' => 'slug',
                        'terms' => 'basic'
                    )
                ),
                'posts_per_page' => -1,
                'orderby'       => 'menu_order',
                'order'         => 'ASC'
            )
        );
    ?>
    <h3 class="text-center"><?php echo $term->title; ?> </h3>

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

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

<?php } ?>

or utilizing this tax_query:

'tax_query' => array(
        array(
            'taxonomy' => 'sections', // Taxonomy title
            'area' => 'term_id',
            'terms' => '1005'
        )
    ),

Both occasions all taxonomies are being displayed and but it all seems proper.

UPDATE 2

This ought to all be working accurately, but it isn’t. Could it be one thing to do with the best way I’ve arrange my custom post type and taxonomies? This is how I’ve bought them:

    add_action( 'init', 'questions_post_type');
operate questions_post_type() {
    $labels = array(
        'title'                  => 'Questions',                                 // This is the Title of the Group
        'singular_name'         => 'Question',                              // This is the person type
        'add_new'               => 'Add New',                                   // The add new menu merchandise
        '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 but, why not create some?',    // This shows if there aren't any entries but
        'not_found_in_trash'    => 'No questions present in Trash',               // This shows if there may be nothing within 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,                                          // that is what order you need it to appear in on the left hand aspect menu
        'menu_icon'             => 'dashicons-editor-help',                     // the icon for the custom post type menu. makes use of constructed-in dashicons (CSS class title)
        'query_var'             => true,
        'rewrite'               => array(
            'with_front' => false,
            'slug' => 'questions'                                                // you can specify its url slug
        ),
        'capability_type'       => 'web page',
        'has_archive'           => true,
        'hierarchical'          => false,
        'helps'              => array( 'title', 'editor', 'revisions')
    ));

}

register_taxonomy( 'sections',
    array('questions'), // for those who change the title of register_post_type( 'instructions', then it's a must to change this
    array(
        'hierarchical'  => true,
        'labels'        => array(
            'title'          => __( 'Sections', 'jointswp' ),         // title of the custom taxonomy
            'singular_name' => __( 'Section', 'jointswp' ),          // single taxonomy title
            'menu_name'     => __( 'Sections', 'jointswp'),          // title as it seems 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' ),   // replace title for taxonomy
            'add_new_item'  => __( 'Add New Section', 'jointswp' ),  // add new title for taxonomy
            'new_item_name' => __( 'New Section', 'jointswp' )       // title 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'
    )
);

[ad_2]

Source link

Leave a Reply