try using 'pre_get_posts' action; http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
example for your question:
function category_archive_sort_posts($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ( $query->is_category ) {
$query->set( 'order', 'ASC' );
}
}
}
add_action('pre_get_posts','category_archive_sort_posts');
Thread Starter
c-m
(@c-m)
So get rid of the loop in my specific category template:
<?php while ( have_posts() ) : the_post() ?>
...
<?php endwhile; ?>
and instead add the following in it’s place?
<?php function category_archive_sort_posts($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ( $query->is_category ) {
$query->set( 'order', 'ASC' );
}
}
}
?>
add_action('pre_get_posts','category_archive_sort_posts');
So get rid of the loop in my specific category template: … and instead add the following in it’s place?
no –
have you read the linked Codex chapter?
the new suggested code needs to be inserted into functions.php of your theme.
Thread Starter
c-m
(@c-m)
Yeah I thought it went in functions.php 😉
I only want it to affect children of category 67. The codex doesn’t go into that detail. I do really need to learn more but it’s slow going.
this seems to work:
function category_archive_sort_posts($query) {
if ( !is_admin() && $query->is_main_query() ) {
$child_cats = get_categories( 'child_of=67' );
if( $child_cats ) {
foreach( $child_cats as $cat ) { $child_cat_ids[] = $cat->term_id; }
}
if ( $query->is_category && $child_cat_ids && is_category( $child_cat_ids ) ) {
$query->set( 'order', 'ASC' );
}
}
}
add_action('pre_get_posts','category_archive_sort_posts');
Thread Starter
c-m
(@c-m)
Yes that works well thanks very much.
What does the if !is_admin part do? It looks like it checks if the dashboard or admin panel is being show. Not sure how that affects things.
What does the if !is_admin part do?
it stops the changes to get applied to the post listings in the admin section.