Skip Navigation

[Resolved] Sort archive view alphabetical or another order

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 15 replies, has 3 voices.

Last updated by Anwar 7 years, 7 months ago.

Assigned support staff: Caridad.

Author
Posts
#30589

Is it possible to set either via the admin panel or some php code to sort archive View's? Currently my archive View is shown in descending order by upload date/time. I would like to sort it by alphabetical order or some other optional order e.g. ascending or random.

is this possible?

Thanks!

#30673

Dear Fuqiang

The query for an WordPress Archive is already defined by WordPress. One thing that you can do is to create a Normal View that allows sorting by any field and insert its shortcode into page.

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#30760

Hi Caridad,

I've created a taxonomy archive view which is only applied my custom taxonomies, so I can't place it's shortcode into any page. It's only visible when clicking on taxonomy link and going into the archive.php template. Is there any other way that I can modify the pre-defined wordpress archive ordering?

Thanks

#31039

Dear Fuqiang,

You can always edit archive.php and add a line to change the query:

<?php query_posts('orderby=title&order=asc'); ?>

You have more information over this function here:

http://codex.wordpress.org/Function_Reference/query_posts

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#31051

Hello Caridad,

Can I place the code you provided anywhere in the archive.php or is there a specific line I need?

Thanks

#31102

Dear Fuqiang,

Before the have_posts();

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#31159

Hi Caridad,

I tried this code:

<?php query_posts('orderby=title&order=asc'); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ): the_post(); ?>
<div <?php post_class('post clearfix') ?> id="post-<?php the_ID(); ?>">

This results in a message "Post not found"

Then I tried this code:

<?php if( have_posts() ) : ?>
<?php query_posts('orderby=title&order=asc'); ?>
<?php while( have_posts() ): the_post(); ?>
<div <?php post_class('post clearfix') ?> id="post-<?php the_ID(); ?>">

And the posts still don't display

Finally I tried this:

<?php if( have_posts() ) : ?>
<?php while( have_posts() ): the_post(); ?>
<?php query_posts('orderby=title&order=asc'); ?>
<div <?php post_class('post clearfix') ?> id="post-<?php the_ID(); ?>">

And only the first post displays full page. I'm not sure which part I'm doing wrong.

Thanks

#31200

Dear Fuqiang,

I think you need to use the $query_string global variable:

<?php query_posts($query_string . '&orderby=title&order=asc'); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ): the_post(); ?>
<div <?php post_class('post clearfix') ?> id="post-<?php the_ID(); ?>">

This is all explained here:
http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#31227

I've applied the code and the result is that every single post gets displayed but all crunched up. Also this results in the archive View I setup to break up, no longer applying the styles the archived posts. Is there a way to keep the View intact but sort the posts in alphabetical order?

Thanks

#31347

Dear Fuqiang,

You only have to add one line, the line about query_posts(). The rest of the lines should stay as you had them. Is this what you did? Can you share a url to view the problem?

Regards,
Caridad

#31649

Yea that's what I did, I pasted <?php query_posts($query_string . '&orderby=title&order=asc'); ?> at the location you suggested and still has a problem. I'll send you a test URL to your email to show you the problem.

Thanks

#32027

Hello Caridad,

I sent you an email on Monday with a test URL, have you received t?

Best Regards

#32111

Dear Fuqiang

I have tested this and you are right, the Views are disabled when changing the query.
You can try another way of doing this with some code in functions.php:

// order archive by title
add_filter('pre_get_posts', 'order_archive_by_title');
function order_archive_by_title($q) {
if ($q->is_tax('uk-category')) {
$q->set('orderby', 'title');
$q->set('order', 'ASC');
}
return $q;
}

This code detects when we are in the uk-category taxonomy page and changes the order to title.

Note that I have already added the code to your theme.

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#32153

Thanks so much Caridad, your support has been amazing!! I've never seen such a great, friendly and efficient support! Thanks again for all the help :),
This plug in is the best plug in I've spent money on

#32236

Dear Fuqiang,

Thanks for your positive feedback. Can we mark the ticket as resolved?

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad