Ask your WordPress questions! Pay money and get answers fast! (more info)

Archive page renders results alphabetically WordPress

  • SOLVED

For some reason my archive page is rendering both taxonomy menu items and the taxonomy results alphabetically and I can't figure out why (or when it happened). See the page at http://seontario.org/resources/.

The menu on the left should be displaying a taxonomy menu, with proper hierarchy, while the post list on the right is showing the posts correctly within their taxonomies, but the hierarchy of the taxonomies has been lost (general, legal structures, procurement and social finance policy should all be under 'policy').

The archive-resource.php is here: [[LINK href="http://pastebin.com/gexc7u3x"]]http://pastebin.com/gexc7u3x[[/LINK]]

There's a function to change the order of another archive to alphabetical, but even after deleting it this page isn't corrected. That function is as follows:


/*
Change the number of posts in the Projects Archive
*/

function seo_modify_num_posts_for_projects($query)
{
if ($query->is_post_type_archive('seo_project') && $query->is_main_query() && !is_admin()) {
$query->set('posts_per_page', 100);
$query->set('orderby', 'title');
$query->set('order', 'ASC');
}
}
add_action('pre_get_posts', 'seo_modify_num_posts_for_projects');


CW

Answers (1)

2014-01-21

John Cotton answers:

Hi Christopher

Is there a reason you don't sort the results in the WP_Query?

I'm assuming that this is the query you want to sort:

$resourcequery = new WP_Query( array(
'post_type' => 'seo_resource',
'tax_query' => array(
array(
'taxonomy' => '$taxonomy',
'field' => 'slug',
'terms' => '$term->slug'
)
),
'posts_per_page' => 100,
'orderby' => 'title',
'order' => 'ASC'
));


Also, you should put a wp_reset_query at the end of the all the loops. It looks like you don't need it, but it's always good practice to put it in for non-loop queries.

JC


Christopher comments:

Pretty sure that wp_query would order the posts, not the taxonomy terms. The post order within the terms is fine.

CW


John Cotton comments:

Ah! So you want to order the posts alphabetically AND have those sorted by taxonomy which is also sorted alphabetically?

Sorry, it's not clear...


John Cotton comments:

Ah! So you want to order the posts alphabetically AND have those sorted by taxonomy which is also sorted alphabetically?

Sorry, it's not clear...


John Cotton comments:

Surely the terms are sorted by name already from the get_terms? If not, then add the args to sort them.

Or have I still missed the point?


Christopher comments:

The alphabetical listing is another post type altogether. I just wanted to show it here in case the pre_get_posts function here might for some reason be interfering with this archive.

I want the resource archive to list the posts, grouped by their hierarchical terms (ie. parent term header > child term > posts, child term > posts, child term > posts; .


Christopher comments:

Sorry accidentally hit submit.

I want the resource archive to list the posts, grouped by their hierarchical terms (ie.

parent term header
> child term header
> posts belonging to child term
> child term header
> posts belonging to child term
> child term header
> posts belonging to child term

parent term header
> child term header
> posts belonging to child term

and so on...


John Cotton comments:

OK - I think I've got it.

What you need is to query the terms in their hierarchy:


$parent_terms = get_terms( $taxonomy, array( 'orderby' => 'slug', 'parent' => 0) );

foreach( $parent_terms as $parent_term) {
echo $parent_term->name;

$child_terms = get_terms( $taxonomy, array( 'orderby' => 'slug', 'parent' => $parent_term->term_id) );
foreach( $child_terms as $child_term) {
echo $child_term->name;

// Do your WP_Query code with slug set to $child_term->slug
}
}


Christopher comments:

Sorry, you lost me. How am I modifying that pastebin?


John Cotton comments:

Try this:

[[LINK href="http://pastebin.com/egRykAsN"]][[/LINK]]


John Cotton comments:

[[LINK href="http://pastebin.com/egRykAsN"]]http://pastebin.com/egRykAsN[[/LINK]]


John Cotton comments:

Sorry, ignore that first link, it's got an error in the code

Use this one:

[[LINK href="http://pastebin.com/uMZ0qbwt"]]http://pastebin.com/uMZ0qbwt[[/LINK]]


Christopher comments:

Interestingly, returns no posts, but also no error.

CW


Christopher comments:

Also, no impact on left hand menu, which remains alphabetical.


John Cotton comments:

Ah - line 41 should be 'term' => $child_term->slug,


Christopher comments:

That didn't change anything unfortunately.


John Cotton comments:

And probably line 36 wants moving to line 46 so that the child term title only displays if there are posts.


John Cotton comments:

Can you re-post your code - I need to see exactly what you have now.


Christopher comments:

http://pastebin.com/u74tZTk1


John Cotton comments:

Looks OK.

Can you add echo '#'.print_r($parent_terms, true).'#'; after the get_terms for parents.


Christopher comments:

Returns

#Array ( ) #


John Cotton comments:

Will that's why you get nothing - no parent terms.

Are the parents definitely top-level parents or are they actually children of another term?

If so, then that parent => 0 needs to have the id of the grandparent.


John Cotton comments:

If you remove the args array on the parent get_terms and keep the print_r we can see exactly what you've got.


Christopher comments:

https://www.dropbox.com/s/pj8g0xilv8g3zia/Screenshot%202014-01-21%2018.56.15.png


John Cotton comments:

Add 'hide_empty' => false to the parent terms args


Christopher comments:

By jove I think that's done it for the post list!


John Cotton comments:

Remove the hide_empty and use 'hierarchical' => true instead.


John Cotton comments:

And you don't need the print_r line any more...


Christopher comments:

Replacing hide empty with hierarchical true returned no results again.

print_r removed.


John Cotton comments:

Really? That should work.

But toggle the hide_empty true/false to check that it's that.


Christopher comments:

hide_empty => true produces the same result > no posts, while hide_empty => false gives results.

CW


John Cotton comments:

Hmm, well keep it in then.

But presumably you don't want the parent titles for terms that have no children?


Christopher comments:

Made a decision with an earlier developer to enforce that all 'resources' had to be in a 'child' term of the taxonomy, so effectively a non-issue (though you're probably correct that it wasn't the right way).


John Cotton comments:

So you have what you want?


Christopher comments:

Not sure how to correct the left menu to handle the hierarchy correctly as well - see first section of pastebin.


John Cotton comments:

Well, the very neat way to do that is to use wp_list_categories with a custom Walker class to adjust the links.

However, you can use the same technique as the posts: you just need to have nested get_terms (ie an outer foreach and an inner foreach on the parent/child terms).


Christopher comments:

Worked it out from your example. Thanks soooo much.

CW