<?php
/**
 * Template Name: FAQ
 *
 * Page template with normal content area including an FAQ section after the post.
 *
 */

 get_header() ?>

  <?php the_post() ?>

  <section id="content" class="articles">
    <header id="post-<?php the_ID(); ?>" <?php post_class('section-header'); ?>>
      <h1 class="title"><?php the_title() ?></h1>
      <div class="intro content">
        <?php the_content() ?>
      </div>
    </header>
    <?php
      /*
      * @Sidebar Content before
      */
      get_sidebar('content-before'); ?>
    <div class="inner">
      <?php
        /**
         * FAQ entries
         * grouped by topic
         * ordered by menu_order      *
         */
        $faq_ids = get_posts(array(
          'post_type' => 'faq',
          'posts_per_page' => -1,
          'fields' => 'ids',
          'meta_query' => array(
            'relation' => 'OR',
            array(
              'key' => '_hide',
              'value' => 1,
              'compare' => 'NOT LIKE',
            ),
            array(
              'key' => '_hide',
              'value' => 'bug #23268',
              'compare' => 'NOT EXISTS',
            )
          ),
        ));
        $topic_term_names = get_terms(array(
          'taxonomy' => 'topic',
          'object_ids' => $faq_ids,
          'fields' => 'names',
	        'orderby' => 'order', // needs plugin https://github.com/stuttter/wp-term-order/
	        'order' => 'ASC',
        ));
      ?>

    <?php if ( !empty($topic_term_names) && !is_wp_error($topic_term_names) ) { ?>
      <?php foreach ( $topic_term_names as $topic_term_name ) { ?>
        <?php
          $faq_options = array(
            'post_type' => 'faq',
            'tax_query' => array(
              array(
                'field' => 'name',
                'taxonomy' => 'topic',
                'terms' => $topic_term_name,
              )
            ),
            'meta_query' => array(
              'relation' => 'OR',
              array(
                'key' => '_hide',
                'value' => 1,
                'compare' => 'NOT LIKE',
              ),
              array(
                'key' => '_hide',
                'value' => 'bug #23268',
                'compare' => 'NOT EXISTS',
              )
            ),
            'posts_per_page' => -1,
            'orderby' => 'menu_order',
            'order' => 'ASC',
          );
          $faq_query = new WP_Query($faq_options);
        ?>

    <?php if ( $faq_query->have_posts() ) { ?>
      <?php while ( $faq_query->have_posts() ) {
        $faq_query->the_post(); ?>

        <?php
          $post_id = get_the_ID();
          $post_type = get_post_type($post_id);
          $post_slug = sandbox_get_the_slug();
          $content = apply_filters('the_content', get_the_content());
        ?>
        <article id="<?php echo $post_slug; ?>" <?php post_class() ?>>
          <h2 class="title accordeon"><a href="#content-<?php echo $post_slug; ?>"><?php the_title() ?></a></h2>
        <?php if ( !empty($content) ) { ?>
          <div id="content-<?php echo $post_slug; ?>" class="inner content">
            <?php echo $content; ?>
          </div>
        <?php } ?>
        </article><!-- .post -->
      <?php } ?>
      <?php wp_reset_postdata(); ?>
    <?php } ?>

      <?php } ?>
    <?php } ?>
    </div>
    <?php
      /*
      * @Sidebar Content after
      */
      get_sidebar('content-after'); ?>
  </section>

<?php get_footer() ?>
