Wordpress query_posts order by date when excluding a category -


i want exclude category , order results date query_posts while preserving existing query parameters , order results date.

according wordpress documentation should add (or over-ride) parameters in following way:

global $query_string; query_posts( $query_string . '&orderby=date&order=asc&cat=-1' ); 

the results right ordering them date doesn't work (asc , desc).

only ordering results works fine:

global $query_string; query_posts( $query_string . '&orderby=date&order=asc' ); 

only excluding category, without ordering results, works fine:

global $query_string; query_posts( $query_string . '&cat=-1 ); 

also including categories , ordering results works fine:

global $query_string; query_posts( $query_string . '&orderby=date&order=asc&cat=2' ); 

of course can work around building array of right categories first, use these categories in query_posts. following:

$include = array();  $categories = get_categories( array('exclude' => 1) );  foreach ( $categories $category ) {     $include[] = $category->term_id; } 

but can't figure out why combination of excluding category , ordering date doesn't work when using query_posts.

tested versions 3.9.1 , 4.0.1 of wordpress. both giving same results.

is bug in wordpress or in code?

edit:

tested wp_query , pre_get_posts based on comments both returning same results query_posts.

wp_query example

$args = array(     'category__not_in'  => array(1),     'post_type'         => 'post',     'orderby'           => 'date',     'order'             => 'asc', );  // query $query = new wp_query( $args ); 

pre_get_posts example

function exclude_category( $query ) {     if ( $query->is_home() && $query->is_main_query() ) {         $query->set( 'orderby', 'date' );         $query->set( 'order', 'asc' );         $query->set( 'cat', '-1' );     } } add_action( 'pre_get_posts', 'exclude_category' ); 

everything works fine until category excluded.

after research think problem caused generated sql query (which same using query_posts or wp_query or pre_get_posts).

when ordering date , excluding category following code:

query_posts( $query_string . '&orderby=date&order=asc&cat=-1' ); 

the following query executed:

select sql_calc_found_rows wp_posts.id  wp_posts  1=1  , ( wp_posts.id not in ( select object_id wp_term_relationships term_taxonomy_id in (1) ) )  , wp_posts.post_type = 'post'  , (wp_posts.post_status = 'publish')  group wp_posts.id  order wp_posts.post_date asc  limit 0, 10  

in case order doesn't work in combination group by. can find more info on in other questions on stackoverflow. example: using order , group together

i think best workaround first build array of categories needs included. use array in query_posts:

$include = array();  // categories except category_id 1 (or other). $categories = get_categories( array('exclude' => 1) );   // loop categories build array category id's foreach ( $categories $category ) {     $include[] = $category->term_id; }  // query posts: order date , include categories array query_posts( $query_string . '&orderby=date&order=asc&cat=' . implode(',',$include) ); 

the generated query (2 , 3 included category id's):

select sql_calc_found_rows wp_posts.id  wp_posts  inner join wp_term_relationships on (wp_posts.id = wp_term_relationships.object_id)  1=1  , ( wp_term_relationships.term_taxonomy_id in (2,3) )  , wp_posts.post_type = 'post'  , (wp_posts.post_status = 'publish')  group wp_posts.id  order wp_posts.post_date asc limit 0, 10 

in case ordering date working fine because of different query buildup (using inner join).


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -