Problem ordering wordpress posts

Asked

Viewed 263 times

1

Hello, good morning.

Guys, I’m having trouble sorting posts in the wordpress categories.

Ex: I have the category news, movies, music, images.. I want to sort the posts of these categories in ascending order. I used the following code

<?php query_posts("order=ASC"); ?>

But instead of just showing posts tied to the category, it pulls all posts from the site.

I also used

$args = array('post_type'=>'post','orderby'=> 'title', 'order' => 'ASC');

The same problem occurred, instead of listing the category posts in ascending order, it lists all posts.

The complete code of category.php that’s the one:

<?php if ( have_posts() ) : ?>
    <header class="container">
    <?php
        the_archive_title( '<h1 class="text-center">', '</h1>' );
        the_archive_description( '<h4 class="text-center">', '</h4>' );
            ?>
    </header><!-- .page-header -->

<section class="categorias">
    <div class="container">
    <?php  while ( have_posts() ) : the_post();?>
        <?php if (get_post_meta($post->ID, 'reservado', true) == 'no'): ?>
        <div class="col-md-4">
            <h3>
                <a href="<?php echo get_permalink();?>">
                <?php the_title();?>
                </a>
            </h3>
            <?php echo odin_thumbnail(400, 200, true, 'minha-classe' ); ?>
        </div>
    <?php endif ?>
    <?php endwhile; ?>
    <div class="col-md-12">
            <div class="text-center">
                <ul class="pagination">
                    <li><?php echo paginate_links(); ?></li>
                </ul>
            </div>
    </div>
<?php else:?>
    <div class="col-md-12 text-center">
        <h1>Não existem pontos nesta semana</h1>
        <img src="/logo.png" class="img-responsive">
        <?php get_search_form(); ?>
    </div>
    <?php endif; ?>
    </div>
</section>

Code in the Pastebin

Thanks for your attention!

  • Leo, do you want to order rising by date or post title?

  • Hello, Giovanni. Need to align with the title, and list only posts in the category linked to the post, and not random.

1 answer

1


Next Leo, assuming that this code of yours is working, I will take advantage of it to make it easier. I added query_posts( $query_string . '&orderby=title' ); to add a sort by title to the existing query.

Take a look at the code below:

<?php 
global $query_string;
query_posts( $query_string . '&orderby=title' );
if ( have_posts() ) : ?>
<header class="container">
    <?php
    the_archive_title( '<h1 class="text-center">', '</h1>' );
    the_archive_description( '<h4 class="text-center">', '</h4>' );
    ?>
</header><!-- .page-header -->

<section class="categorias">
    <div class="container">
        <?php  while ( have_posts() ) : the_post();?>
            <?php if (get_post_meta($post->ID, 'reservado', true) == 'no'): ?>
                <div class="col-md-4">
                    <h3>
                        <a href="<?php echo get_permalink();?>">
                            <?php the_title();?>
                        </a>
                    </h3>
                    <?php echo odin_thumbnail(400, 200, true, 'minha-classe' ); ?>
                </div>
            <?php endif ?>
        <?php endwhile; ?>
        <div class="col-md-12">
            <div class="text-center">
                <ul class="pagination">
                    <li><?php echo paginate_links(); ?></li>
                </ul>
            </div>
        </div>
    <?php else:?>
        <div class="col-md-12 text-center">
            <h1>Não existem pontos nesta semana</h1>
            <img src="/logo.png" class="img-responsive">
            <?php get_search_form(); ?>
        </div>
    <?php endif; ?>
</div>
</section>

Code also available on Pastebin

Explanation

When you tried to use <?php query_posts("order=ASC"); ?> you were replacing query_posts to order=ASC then was bringing everything ordered by date.

Using the query_posts( $query_string . '&orderby=title' ); you are concatenating to query_posts next to the orderby.

  • Apparently it worked, Giovanni. I just didn’t get it right, why did 36 jump to 57 and then 36. Would it be because of the order of the letters? All posts start with numbers 1 - Rua Brasil, 2 - Rua Matilda, 3 - Rua Amaral... http://prntscr.com/cne0g9

  • @Leonardo really is strange, try to run this query in your database SELECT 'post_title' FROM 'wp_posts' ORDER BY 'post_title'&#xA; It may not be wp_posts Woe to you if you’ve changed

  • If he returns wrong also in the bank, then it would be the case to research, because it never happened to me this. But check if in the next few pages he is also reversing the last one with the penultimate!

  • It was an error in the bank, as you said, I also did not understand what you’re getting, but still thank you, Giovanni, the advance was great.

  • @Leonardo Nothing does not! But if you need help to understand this wonder ai of SQL, I advise you to create a new question just to deal with it!! In case I have helped and the doubt of this question is completed, evaluate me positively and mark the "check" in my reply!

  • You can leave, Giovanni. I will give a study further sinking. It helped a lot, thank you for the force.

Show 1 more comment

Browser other questions tagged

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