Posted on

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’);

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-> 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’);

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’) ;




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s