0

I'm setting up a FAQs page. I've created a custom post type called "Questions" with a custom taxonomy called "Sections". Within the sections I've created several categories:

  • General
  • Rules
  • Payment

...etc

There's 90 questions all categorised within one of the sections. I'd like to output the loop/query so that each question sits under its section heading. If new sections or questions are added they will automatically get generated in the loop.

The output would be something like this:

General

  • Question one
  • Question two
  • Question three

Rules

  • Question four
  • Question five
  • Question six

Payment

  • Question seven
  • Question eight
  • Question nine

... etc etc

  • Ok, so you can set up a template for your archive called archive-questions.php. Look up all "section" taxonomies associated with your questions CPT, then do a foreach loop where you output the section name as the heading, and do a query for all questions that have that particular taxonomy term set. – WebElaine May 19 '17 at 16:01
1

Check the below code-

// 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() {
    $terms = get_terms('sections'); // Taxonomy name
    echo '<ul>';
    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
            )
        );
        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>';
}

Here we are initiating a shortcode called faq_page_content. Load this block of code through your plugin or functions.php and paste [faq_page_content] shortcode inside your desired page where you wanna show the FAQ's. You'll get your needed output.

Hope that helps.

  • That's great, thanks! I've removed the function and just used it within the loop on the archive page, but it's good to see how it can be used as a shortcode, very useful. – Mr Jonny Wood May 19 '17 at 19:28
  • Quick follow up. Is there anyway to output the content of each post? As the content (the answers to the questions in this case) are only one sentence I don't want to link to the post, I simply want to display it underneath. I tried echo $post->post_content(); but this didn't work :( – Mr Jonny Wood May 20 '17 at 8:40
  • 1
    Try it by removing the parenthesis. I mean like this - $post->post_content. – CodeMascot May 20 '17 at 8:42
  • My last question on this - if I wanted to output only the questions related to a specific category such as 'Payment' is there a way to filter further down using the ID of that category? – Mr Jonny Wood May 20 '17 at 8:48
  • I'm not clear about your question. Would you please elaborate it a little bit ? If you think that the comment form is not enough please feel free to ask another question with the above answer code and give me the link. – CodeMascot May 20 '17 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.