Problem when ordering posts in wordpress

1

Hello, good morning.

Well, I'm having trouble sorting posts in 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 showing just the posts linked 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 for category.php is this:

<?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 pastebin

Thanks in advance for your attention!

    
asked by anonymous 28.09.2016 / 15:44

1 answer

1

Next Leo, based on the premise that his code is working, I will take advantage of it to facilitate. I added query_posts( $query_string . '&orderby=title' ); to add a sort order by 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 the query_posts with order=ASC so you were bringing everything sorted by date.

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

    
28.09.2016 / 16:21