0

I cant change the way things are, so I cant use custom meta...

I have terms that are years and I have to display the archive in order (newest to oldest-in terms, not published date) For example the terms are 2007,2008,2009. I need the posts to display first the 2009 ones then the 2008, then the 2007.

I prefer to use a function than the query call. Why? I am using a generic archive.php page, not using one for post types. I'd like to keep it that way.

I try to add a function to the theme to change the way some posts are displayed with no success. Please help me out! :D

add_action('pre_get_posts', 'my_change_sort_order');

function my_change_sort_order($query)
{
if (is_post_type_archive($post_types = 'zee_publications')):
    $query->set('order', 'desc');

    $query->set(get_terms(array(
        'taxonomy' => 'cat_publications',
        'orderby',
        'slug'
    )));
endif;
};
6
  • It's a great example of what I call "wishful coding" - you write some random code that makes no sense at all and wish that some magic will happen and it will work. The part $query->set(get_terms(array(... makes no sense at all. This method assumes first param is name of field and second is new value - you pass only one value - list of terms... Commented Jul 30, 2018 at 17:59
  • Please take a look at rules of this site. Before adding a question you should do some research by yourself. Then you should search for similar questions (you clearly haven’t, because almost the same question was asked one or two weeks ago and there are plenty other questions regarding the same issue). Then you called me a douche for giving you an answer (in form of link to exactly the same question...) Great job, really... Commented Jul 30, 2018 at 18:23
  • it is not the same question. I want to alter the query in functions.php Commented Jul 30, 2018 at 18:24
  • read the answers in that question with some understanding. There is no native way to sort by terms. It wont be effective and you can’t achieve it using pre_get_posts. It is clearly stated in first answer for that question. Commented Jul 30, 2018 at 18:26
  • I have read tons of questions about this, and it can be done, according to a lot of people, ex. wordpress.stackexchange.com/questions/69302/… ...but nobody uses the functions.php, so...i should delete my question then, Mr.? Commented Jul 30, 2018 at 18:28

1 Answer 1

0

First things first... Why it doesn't work?

Your code doesn't work, because you use WP API incorrectly...

Method set takes exactly two arguments:

set( $query_var, $value )

Set a named query variable to a specific value.

So how do you use it in your code?

$query->set( get_terms( array(
    'taxonomy' => 'cat_publications',
    'orderby',
    'slug'
) ) );

There is only one argument (so it's $query_var - name of variable) and you pass get_terms result in there...

So what is that function doing? It retrieves the terms in a given taxonomy or list of taxonomies (and it returns a list of terms).

OK, so basically your current code is creating some custom query variable with name set to array containing terms and no value.

So it should be pretty clear right now, why this code doesn't do what you want it to...

So how to order posts by terms using WP_Query?

Let's take a look at Order & Orderby Parameters. As you can see, you can order by many properties, but... Terms are not among these properties.

This is caused by database structure - ordering posts by terms wouldn't be efficient and it is so rare, that there is no point in implementing it.

So how to do it? You can't - there is no way to use WP_Query to order posts by terms. It doesn't matter if you create your own WP_Query loop, or if you use pre_get_posts (as you do) to modify global $wp_query instance...

So is it really impossible?

No, of course it is possible. You can always take control over raw SQL that given WP_Query is creating and modify it.

cenk posted an example of such query in his answer here: https://wordpress.stackexchange.com/a/136825/34172

Of course you don't have to use it as raw query. You can use posts_clauses filter to add missing SQL parts to the query generated by your WP_Query.

But it won't be efficient and it won't be easy to implement it correctly. There are many boundary conditions and many things can go wrong (what if some posts have multiple terms, etc.)

And you can also cheat a little bit

WP_Query can order posts by meta value. So you can store term name as meta value with save_post hook and then sort posts by this meta value...

Not the answer you're looking for? Browse other questions tagged or ask your own question.