How to show a listing of all posts on any page

Asked

Viewed 1,929 times

1

I’m wanting to make a list to get the most commented posts, but my problem is that I can’t call the right listing always, here’s the code:

        <ul>
        <?php 
            global $query_string;
            query_posts($query_string.'&posts_per_page=-1, post_status=publish');

            if (have_posts()): while (have_posts()): the_post();

            $post_name = get_the_title();
            $post_url  = get_permalink();
        ?>
            <li>
                <a class="transition-2s" title="<?php $post_name; ?>" href="<?php $post_url; ?>" rel="bookmark"> 
                    <span class="reclink"><?php echo $post_name; ?></span>
                </a>
            </li>
        <?php 
            endwhile; 
            endif;
            wp_reset_query();
        ?>
    </ul>

On the home page works very well, but on a specific page, such as a post for example, this code only lists a single item, which is the post in question.

I want to release this code in the footer, so that it always appears, regardless of the page the reader is.

Thank you in advance!

1 answer

3

There are some problems here, in the title you say you want to list all, but in the description you’re saying you want to list the most comments...

Anyway, it’s not gonna work anyway because you’re getting the $query_string which changes from one page to another.

In addition not to be used query_posts(), just read in the documentation that the correct is to use pre_get_posts to change the loops that already exist and the additional ones should be done with WP_Query.

To list all you could use:

<ul>
    <?php 
        $all_posts_query = new WP_Query( array(
            'nopaging' => true, // deve ser usado no lugar de posts_per_page -1
            'post_status' => 'publish'
        ) );

        if ( $all_posts_query->have_posts() ) :
            while ( $all_posts_query->have_posts() ) :
                $all_posts_query->the_post();
            ?>

            <li>
                <a class="transition-2s" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>" rel="bookmark"> 
                    <span class="reclink"><?php the_title(); ?></span>
                </a>
            </li>

            <?php endwhile;
        endif;
        wp_reset_postdata();
    ?>
</ul>

It’s pretty simple now also change that to catch the most commented posts, as just sort by the number of comments.

<ul>
    <?php 
        $all_posts_query = new WP_Query( array(
            'nopaging' => true, // deve ser usado no lugar de posts_per_page -1
            'post_status' => 'publish',
            'orderby' => 'comment_count', // Ordena pelos mais comentados
            'order' => 'DESC' // Começa pelos números maiores, exemplo 10, 9, 8...
        ) );

        if ( $all_posts_query->have_posts() ) :
            while ( $all_posts_query->have_posts() ) :
                $all_posts_query->the_post();
            ?>

            <li>
                <a class="transition-2s" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>" rel="bookmark"> 
                    <span class="reclink"><?php the_title(); ?></span>
                </a>
            </li>

            <?php endwhile;
        endif;
        wp_reset_postdata();
    ?>
</ul>

Well that’s it, now you can do both consultations.

However, I don’t always recommend loading all posts like this at once, because if you have too many posts it will cause performance issues. In the case of the most commented I recommend using posts_per_page of 10 or something like that.

  • I understand, thank you for your help! You are right, I will have serious performance problems. I was trying to do a gambit, but it will end up being worse. My intention is only the most commented, but I use the comments of facebook as I asked in that post and told me that I would have to use the facebook API, but as I do not know, I was trying to get around, but it seems that it is more bad than good... Anyway, I really appreciate it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.