Skip Navigation

[Resolved] Order posts in one view from two categories and alphabetically

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View that lists results in alphabetical order. I would like to show posts with one specific taxonomy term first, in alphabetical order, followed by posts without the specific taxonomy term, in alphabetical order.

Solution: Here's how I would set this up with a custom field:
- Create a "radio" type custom field and give it two options: 0 (not special) and 1 (special). Make 0 the default value. Add this custom field to the person post type.
- Edit each person post and set the radio field to the appropriate value.
- Edit the View of people and modify the Order By configuration. If you cannot see the "Ordering" configurations, scroll to the top right corner of the screen and click "Screen Options". You can activate the "Ordering" panel here. Then select the radio custom field for the primary sort, and select the person title for the secondary sort. See the attached screenshot.

Relevant Documentation:
https://toolset.com/documentation/user-guides/front-page-filters/
https://wp-types.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#902026

Tell us what you are trying to do?

I would like to display multiple people alphabetically. So far so good. Now I have some "special" people, that should appear first. That's a group of 5-6 people. They can be sorted alphabetically within that group just before everybody else.

Is there any documentation that you are following?

Not directly, but I think I saw a video where you loaded a post type first and then added other content if the rows weren't full.I assume this technique can be used in this case as well.

What is the link to your site?

hidden link

***

If that's not possible, I can make two separate Views and give each of them a seperate title to visually separate the two groups. But it would be best if we could have all people in the same list without any visual break between them.

Here is the code of the View I use:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<div class="ifb-personenverzeichnis">
<wpv-loop>
<div class="ifb-personenverzeichnis-container">[wpv-post-body view_template="Person"]</div>
</wpv-loop>
</div>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

Thanks a lot for helping me out. I hope by the end of this project I have it all figured out and don't have to ask so much 😉

Best,
Marcial

#902154
Screen Shot 2018-05-22 at 12.36.04 PM.png

Hi, I can help you set up a View that is ordered by a custom field value, but ordering by taxonomy term is not a built-in Views feature and would require some significant custom code. So if it's possible to use a custom field to designate a "special" person instead of a category or taxonomy term, this approach might work. Here's how I would set this up with a custom field:
- Create a "radio" type custom field and give it two options: 0 (not special) and 1 (special). Make 0 the default value. Add this custom field to the person post type.
- Edit each person post and set the radio field to the appropriate value.
- Edit the View of people and modify the Order By configuration. If you cannot see the "Ordering" configurations, scroll to the top right corner of the screen and click "Screen Options". You can activate the "Ordering" panel here. Then select the radio custom field for the primary sort, and select the person title for the secondary sort. See the attached screenshot.

#906846

Hi Christian

I did as you said, but now it only display those who have the value set to "Yes, they are special". If I disable the sorting, it shows all people again.

I see that on your screenshot you have a filter enabled. I don't have that. Do I need to do this?

Thanks for your help.

Best,
Marcial

#906990

No filter is necessary. To be clear, you must resave all User profiles, including the "not special" Users. Otherwise, they will not have a value set for this custom field and sorting will filter out those Users. If you have saved even the not special Users, then there could be something else going on and I'll need to take a closer look. Please provide login credentials in the private reply fields here and I can investigate.

#907455

Hi Christian

My bad, I didn't know it's necessary to save all posts again. I'm just wondering, in case I have a very large number of posts, is there a way to save all of them at once without having to go into each of them separately?

So the order is almost correct. But now I need them to sort alphabetically as secondary rule. Our post titles are like this: PRENAME LASTNAME. Therefore I created a new field "last name" that I would like to use. But I don't seem to have the option to use a custom field within the rules I defined for the "special" and "not special" team members. How can I achieve that?

Thanks for your help!

Best,
Marcial

#907930

is there a way to save all of them at once without having to go into each of them separately?
It's not possible to bulk-edit custom field values, but you could write a custom SQL script that inserts the default values automatically. There are some other plugins out there that claim to give you the ability to modify custom field values in a bulk edit screen, but I can't guarantee their effectiveness.

But I don't seem to have the option to use a custom field within the rules I defined for the "special" and "not special" team members.
There's no easy way to create a secondary sort using a custom field within wp-admin, it has to be done with custom code. We offer the wpv_filter_query API to help you apply custom sorting criteria like this. Replace 12345 with the numeric ID of this View, replace is-special with the slug of your special custom field, and replace last-name with the slug of your last name custom field. Then add this code to your child theme's functions.php file:

add_filter( 'wpv_filter_query', 'order_by_two_custom_fields',199,3 );
function order_by_two_custom_fields( $query_args, $views_settings, $view_id) {
  $view_ids = array( 12345 );
  if (in_array($view_id, $view_ids)){
    $args = array(
      'meta_query' => array(
        'relation' => 'AND',
        'rad-clause' => array(
          'key' => 'wpcf-is-special',
          'compare' => 'EXISTS',
          'type' => 'NUMERIC'
        ),
        'begin-clause' => array(
          'key' => 'wpcf-last-name',
          'compare' => 'EXISTS',
          'type' => 'STRING'
        )
      ),
      'orderby' => array(
        'rad-clause' => 'ASC',
        'begin-clause' => 'ASC'
      )
    );
  }
  return array_merge($query_args, $args);
}

More information about this API is available here:
https://wp-types.com/documentation/programmer-reference/views-filters/#wpv_filter_query