Question

The following does not work right out of the box and the page takes forever to load, it creates a lot of Apache processes and consumes memory and CPU like crazy

<?php

/**
 * Template Name: Custom forum index
 */

get_header();


?>
            <div id="content" role="main">

                <?php //do_action( 'bbp_template_notices' ); ?>

                <?php
                $args = array(
                   'post_type' => 'forum',
                   'post_status' => 'publish',
                   //'meta_key' => 'age',
                   'orderby' => 'title',
                   'order' => 'ASC',
                   // 'meta_query' => array(
                        // array(
                           // 'key' => '_bbp_topic_count'
                        // ),
                        // array(
                            // 'key' => '_bbp_reply_count'
                        // ),
                        // array(
                            // 'key' => '_bbp_last_active_time'
                        // ),
                        // array(
                            // 'key' => '_bbp_last_topic_id'
                        // )
                    // )
                );
                $query = new WP_Query($args);

                while ($query->have_posts()) : the_post(); ?>

                    <div id="forum-front" class="bbp-forum-front">
                        <h1 class="entry-title"><?php the_title(); ?></h1>
                        <div class="entry-content">

                            <?php //the_content(); ?>

                            <?php //bbp_get_template_part( 'content', 'archive-forum' ); ?>

                        </div>
                    </div><!-- #forum-front -->

                <?php endwhile; ?>

            </div><!-- #content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>  

The underlying motivation is that that I'm trying to create a custom forum index page on which forums are laid out alphabetically based on their names. Strangely, such functionality does not come with bbPress

As you see above, I attempted to use WP_Query() to loop through the post with type "forum". What could have gone wrong with such approach? What do I need to do use to WP_Query() here?

Était-ce utile?

La solution 2

What was wrong is the_post(). It should be $query->the_post(); instead.

So the correct loop would be

<?php  
//...  
while ($query->have_posts()) : $query->the_post(); ?>

                <div id="forum-front" class="bbp-forum-front">
                    <h1 class="entry-title"><?php the_title(); ?></h1>
                    <div class="entry-content">

                        <?php //the_content(); ?>

                        <?php //bbp_get_template_part( 'content', 'archive-forum' ); ?>

                    </div>
                </div><!-- #forum-front -->

            <?php endwhile; ?>

Autres conseils

Because of use of custom WP_Query, you need to specify wp_reset_query(); after your endwhile; keyword. According the Wordpress documentation here, "This function destroys the previous query used on a custom Loop. Function should be called after The Loop to ensure conditional tags work as expected."

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top