Archive Layouts: Order by a Meta Value

This topic popped up occasionally. I’d like to bring it back to focus.

We can easily make custom sorting inside a Query string, with orderby=meta_value&meta_key=some_key
Then we can for example use ACF fields to instruct the looper about the exact ordering.

This however, is not possible on the Archive layouts. It would be amazing if we could have Layout settings somewhere where we can choose how the layout is handling ordering, and if we choose “Meta value”, then we can enter our custom meta key there to be used.

Thanks!

5 Likes

Yes please :raised_hands:

1 Like

I’ve been doing this with PHP but this would be awesome.
Always good to make things quicker and easier for people.

2 Likes

most definitely, yes please

1 Like

Definitely a +1

1 Like

Sounds like a cool feature. Just so I’m on the same page. If orderby=meta_value&meta_key=some_key was in your URL your sorting is fine? In a way, are we trying to set the default ordering of an archive? Or is this a request to add the “Meta” value to the provider controls? Or all of the above? Thanks!

image

Hi @charlie!

So in my case, I made a FAQ Custom post type and I wanted to order them in a custom way. For that reason, I made an ACF field where I can enter an arbitrary number on individual FAQ. The lower the number, the higher the post is listed. Thanks to this I can decide which FAQ is displayed first, which one is second etc.

That works great when Looper provider is used, with a custom query string, on a page for example.

Archives on the other hand, don’t need a provider so we don’t have an opportunity to use a query string or having additional setting natively.

One such setting on Archive pages could be an option to set a meta key by which the sorting would be done. This key can come from ACF or possibly some other sources. Whatever can allow custom sorting of archives is fine.

Thanks!

2 Likes

Just to throw another use case into the mix,

I create CPTs quite often for client events, which have start dates in the custom fields… I would like to be able to sort an archive based on those custom start dates (rather than published date)

3 Likes

This would be a cool feature. As with everything. The purpose of using a builder, in my opinion, is we want to speed up our work flow. Making things easier to do is always a good idea :slight_smile:

Here is one way to do this which I neglected to add in my previous comment. You can put conditionals around the query to only target specifics. Or change the query for different sorting options. Depending on what you want to do you can also look at pre_get_posts.

add_filter('posts_orderby','custom_post_sorting', 10, 2);
function custom_post_sorting ( $orderby, $query ){
    global $wpdb;

    if(!is_admin() && is_search()) 
        $orderby =  "{$wpdb->prefix}posts.post_date DESC";

    return  $orderby;
}
2 Likes

I think I’m tracking more and I see a similar request for this from a year or two ago. The request being an order by option for Dynamic Content Looper.

This is more general looper sorting / pre processing sorting which I think we can bring into a number of places. Even a Dynamic Content call itself. So I guess my idea is adding in a generic order creator similar to the query builder which could be based on array values or a custom sorting function. And these same controls exist for the settings section of an archive controlling the default provider. A part two being more similar to Urchins code which actively changes the query. It sounds like a great feature, let me know if this tracks with you, and thanks for the comments.

5 Likes

My early morning brain thinks this sounds perfect, but my early morning brain may not be fully understanding the solution you are suggesting…

2 Likes

Sounds brilliant.

Another thought would be to have a dynamic section in the theme options that allows you to set order, paging etc. per post type for archive pages :slight_smile:

As you create new CPT’s they become available in the options .

2 Likes

We’ll mock up some idea for this after 6.3 which we are planning on starting the beta tuesday or wednesday. Will probably start by adding this into the Dynamic Content looper first too and see how it does. Thanks for brainstorming with me and have a great weekend everybody.

6 Likes