Filtering custom post type query

Yes, you can do that. Just remove the first foreach and give the General category ID and change the code like below-

// Initiating shortcode. Place this code to any of your page to get your desired output.
add_shortcode( 'faq_page_content', 'the_dramatist_faq_page_content');
/**
 * Rendering function
 */
function the_dramatist_faq_page_content() {
    echo '<ul>';
    $posts = get_posts(
        array(
           'post_type' => 'questions', // Post type
           'tax_query' => array(
               array(
                   'taxonomy' => 'sections', // Taxonomy name
                   'field' => 'term_id', // For taxonomy ID
                   'terms' => 'the-ID-of-the-taxonomy' // Here you need to pass the term ID, in you case General taxonomy term ID
               )
           ),
           'posts_per_page' => -1
        )
    );
    echo '<li>' . $term->name;
    echo '<ul>';
    foreach($posts as $post) {
        echo '<li><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></li>';
    }
    echo '</ul></li>';
    echo '</ul>';
}

Or for your current page template code-

<?php
$posts = get_posts(
    array(
        'post_type' => 'questions', // Post type
        'tax_query' => array(
            array(
                'taxonomy' => 'sections', // Taxonomy name
                'field' => 'term_id', // For ID
                'terms' => 'the-ID-of-the-taxonomy' // Here you need to pass the term ID, in you case General taxonomy term ID
            )
        ),
        '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>

Look carefully, here we are using taxonomy terms ID for getting post assigned for a taxonomy term, OK ? –

'tax_query' => array(
    array(
        'taxonomy' => 'sections', // Taxonomy name
        'field' => 'term_id', // For ID
        'terms' => 'the-ID-of-the-taxonomy' // Here you need to pass the term ID, in you case General taxonomy term ID
    )
)

You can also define a taxonomy term by term slug also. And for that you need to modify the previous block like below-

'tax_query' => array(
    array(
        'taxonomy' => 'sections', // Taxonomy name
        'field' => 'slug', // For ID
        'terms' => 'the-slug-of-the-taxonomy' // Here you need to pass the term slug, in you case General taxonomy term slug
    )
)

Hope that helps !!!

Leave a Comment