Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Sort by the order from variable WordPress

  • REFUNDED

I would like to have the query in `$keep` sorted by the order that is in the variable `$groups_separated`

Here is a small section:

$args=array(
'post_status' => 'publish',
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product_categories',
'terms' => explode(',', $groups_separated),
'field' => 'slug',
)
),
'posts_per_page' => 30
);

if(is_tax()) $args = array_merge( $wp_query->query, $args);

//keep posts
$keep = query_posts($args);


Here is my full code: http://pastebin.com/Zzrz6PmT

Answers (3)

2013-10-15

Remy answers:

So what you want to do is order your results by the taxonomy values ?

If correct, since it's not something you can do with the WP_Query system, you need to do a custom SQL query. This post can be helpful for your specific request : http://scribu.net/wordpress/sortable-taxonomy-columns.html


jdm comments:

The line:
$keep = query_posts($args);
is changing the orderby to ID. The only reason that I wanted to do it by $groups_separated is because that will always be in the correct order. I will take a look at your link.


Remy comments:

Order by ID is the default order in a WP Query, that's why you have your results sorted like this. Maybe you can also find something in the orderby section of the wp_query codex page : http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters


jdm comments:

Just a thought. Is it possible to disable default sorting? If that is possible wouldn't the loop just display the results in the order that $args has them in?

2013-10-15

Arnav Joy answers:

write this code in functions.php


add_filter('posts_clauses', 'posts_clauses_with_tax', 10, 2);
function posts_clauses_with_tax( $clauses, $wp_query ) {
global $wpdb;

$taxonomies = array('product_categories');
if (isset($wp_query->query['orderby']) && in_array($wp_query->query['orderby'], $taxonomies)) {
$clauses['join'] .= "
LEFT OUTER JOIN {$wpdb->term_relationships} AS rel2 ON {$wpdb->posts}.ID = rel2.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} AS tax2 ON rel2.term_taxonomy_id = tax2.term_taxonomy_id
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
";
$clauses['where'] .= " AND (taxonomy = '{$wp_query->query['orderby']}' OR taxonomy IS NULL)";
$clauses['groupby'] = "rel2.object_id";
$clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
$clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
}
return $clauses;
}


Arnav Joy comments:

check this link also.

http://wordpress.stackexchange.com/questions/69302/sort-posts-by-custom-taxonomy-name


jdm comments:

No change in sort, not sure why.

2013-10-15

Sébastien | French WordpressDesigner answers:

The code to sort your custom posts "products" by taxonomy name

<?php
$terms = get_terms('product_categories');
foreach($terms as $term) {
$posts = get_posts(array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product_categories',
'field' => 'slug',
'terms' => $term->slug
)
),
'numberposts' => -1
));
foreach($posts as $post) {
// do what you want to do with the posts here
echo "example : " . $post->post_title . "<br/>";
}
}
?>


jdm comments:

I am not sure how to implement this into what I have existing