How add order by, where and inner to wp_query, to order by subcategori

add_filter(‘posts_orderby’, ‘edit_posts_orderby’);
add_filter(‘posts_where’, ‘edit_posts_where’);
add_filter(‘posts_join_paged’,’edit_posts_join_paged’);

function edit_posts_join_paged($join_paged_statement) {
global $wpdb;
$pais = get_cat_ID(“Pais”);
$join_paged_statement = ” INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.parent=$pais) INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id=$wpdb->terms.term_id) “;
return $join_paged_statement;
}
function edit_posts_where($where_statement){
global $wpdb;
$where_statement = ” AND $wpdb->term_taxonomy.taxonomy = \’category\’ “;
return $where_statement;
}
function edit_posts_orderby($orderby_statement) {
global $wpdb;
$orderby_statement = ” $wpdb->terms.name ASC, post_title ASC “;
return $orderby_statement;
}

Example to use;

// The Query
$arre = array();
if(is_day()) $arre[‘day’] = get_the_time(‘d’);$arre[‘monthnum’]= get_the_time(‘m’);$arre[‘year’]= get_the_time(‘Y’);
if(is_month()) $arre[‘monthnum’]= get_the_time(‘m’);$arre[‘year’]= get_the_time(‘Y’);
if(is_year()) $arre[‘year’]= get_the_time(‘Y’);
$arre[‘nopaging’]=true;

add_filter(‘posts_join_paged’,’edit_posts_join_paged’);
add_filter(‘posts_orderby’, ‘edit_posts_orderby’);
$query = new WP_Query($arre);
remove_filter(‘posts_join_paged’, ‘edit_posts_join_paged’) ;
remove_filter(‘posts_orderby’, ‘edit_posts_orderby’) ;

 

REFERENCE: http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_orderby

Advertisements