Skip Navigation

[Resolved] Sorting by letters and numbers – put together in groups

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

Problem:

A user asked how to show the list of posts from a post view, grouped by the starting letter or number.

Solution:

Shared the steps to create a post view that also uses custom CSS and JS code for this grouping.

Relevant Documentation:

n/a

100% of people find this useful.

This support ticket is created 2 years, 9 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 16 replies, has 2 voices.

Last updated by kristenM 2 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#2321015

Tell us what you are trying to do?
I'm trying to make a view where posts starting with 1, 2, 3... and A, B, C... are sorted by the first number/letter.

Is there any documentation that you are following?
I have tried to follow this tutorial: https://toolset.com/forums/topic/sorting-by-letters-und-numbers-at-the-same-time/
but I can't find where I put the php-script in this post.

I have years ago tried something similar in this support-post: https://toolset.com/forums/topic/alphabetical-and-numeric-search-buttons/
At that time Toolset had another kind of View-generator - much more complicated - but I can't find it anymore.

Is there a similar example that we can see?
I would like the view to look like this page: hidden link
- but without buttons (they are only nice to have :-))
The page starts with showing the number "3" and the only company/post shown - and then "A" and so on.

What is the link to your site?
I'm working on a development-site here: hidden link

Can you help me telleing me where to find the old/extended version of View?

Thank you in advance and kind regards.

Kristen

#2322501

Hi Kristen,

Thank you for contacting us and I'd be happy to assist.

I've checked the page that you've shared and the view's results seem to be ordered correctly, first by numbers and then by letters.

The PHP code snippets (similar to the one shared in the other ticket) can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

To enable classic/legacy views, please go to WP Admin -> Toolset -> Settings -> General and select the "Show both the legacy and Blocks interface" option for the "Editing experience". After that, you'll be able to manage the classic/legacy views from WP Admin -> Toolset -> Views.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2322563

Hi Waqar.
Thank you for your reply.
The page I showed you (hidden link ) is an examplepage and not the site I'm working on.
The page I'm working on is hidden link
I will try your instructions over the next few days and if I get som questions I will write again in this ticket.
Thank you in advance.
Kind regards
Kristen

#2322629

Thanks for writing back and please take your time.

As for showing the results grouped by the starting number/letter (as shown on the example page), here is what I would recommend:

1. You can add a new custom taxonomy "Starting Letter" and in it, add all the individual numbers and letters as the taxonomy terms.

2. While adding/editing posts, you can select the correct taxonomy term which is the first letter/number of the post title.

3. For the page where you'd like to show this grouped list, you'll first create a taxonomy view (parent), that will show all the terms for this "Starting Letter" taxonomy.

4. Next, inside the loop of the taxonomy view, you'll add a post view (child) to show only the results, filtered by the taxonomy term coming from the parent taxonomy view.

This way, you'll have two lists, nested within each other, and since nesting of views and taxonomy views are not supported by the blocks-based views, you'll use classic/legacy views for this.

We have a guide on the topic of nested views at:
https://toolset.com/documentation/legacy-features/views-plugin/using-a-child-view-in-a-taxonomy-view-layout/

#2323113
Toolset-legacy Views error.png

Hi again Waqar.
I'm trying to get to use the legacy features like you mention in reply #2322501 - but Im' not getting the permission as you can see in my uploaded image.
Do you have any idea of what I'm doing wrong?
Kind regards
Kristen

#2323747

Hi again Waqar.
I have created a new View and now I can use the legacy features.
I will return if I bump in to other problems.
Kind regards
Kristen

#2323891

Thanks for the update and glad that it is sorted now.

You're welcome to mark this ticket as resolved and start a new one for each new question or concern.

#2325173

Hi again Waqar.
Unfortunately I don't seem to get the result I'm looking for.
I was looking for a way to sort a number of company names (that I'm importing from another site on a daily basis) by the first mumber or letter in their Company Name. As an example:

3:
365 Learning, 3rd Best Buy etc.

A:
Alhambra Almonds , Anathomy Gym etc.

B:
Boston Bakery, Blakes Garage etc.

I was wondering if I instead could make is as a view per number/letter with the condition that if there ain't any companies registered under a certain letter it won't be shown.

But I can't get the queryfilter only to choose the Companies starting with only A og B or...

Is there a way to do that or should I end this ticket and start a new ticket instead=

Kind regards

Kristen

#2325939

Thanks for writing back.

If multiple views/queries are used for each starting character, it can have an impact on the performance. The page loading time will increase, as the number of posts will grow over time.

On my test website, I was able to achieve this, using a single post view, and some custom code. Here are the steps:

1. I registered a custom shortcode, that gets the current post's title and returns only the starting character:


add_shortcode('start_with_class', 'start_with_class_func');
function start_with_class_func($atts) {
	return strtoupper(substr(get_the_title(), 0, 1));
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

2. Next, I created a classic/legacy view from WP Admin -> Toolset -> Views.

3. In the view's settings, I selected the post type to show the results from and set the order settings to show results ordered by post title in ascending order.
( screenshot: hidden link )

4. In the view's "Loop Editor", I structured the output in this format:
( screenshot: hidden link )


[wpv-layout-start]
  [wpv-items-found]
  <div id="new-container"></div>
  <div id="orig-container" style="display:none;">
    <!-- wpv-loop-start -->
    <ol class="wpv-loop js-wpv-loop">
      <wpv-loop>
        <div class="orig-item-container" data-start-with="[start_with_class]">
        [wpv-post-title]
        </div>
      </wpv-loop>
    </ol>
    <!-- wpv-loop-end -->
  </div>
  [/wpv-items-found]
  [wpv-no-items-found]
    <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
  [/wpv-no-items-found]
[wpv-layout-end]

Notes:
- In the layout, there are two main divs containers. The div with id "new-container" is empty and the div with id "orig-container" contains the loop of the results coming from the view.

- Each individual result item is enclosed in a div with class "orig-item-container" and "data-start-with" attribute holds the starting character for the post, through the custom shortcode registered in step 1.

- in this example, I'm only showing the post title of the current result using the shortcode [wpv-post-title], but you can show any information, using the respective shortcodes ( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/ ).

If you'd prefer to show the individual result item using the Blocks editor, you can also create a new content template, design the individual result item and then load this template inside this view's loop, using the "wpv-post-body" shortcode:
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-body )


..........
<wpv-loop>
  <div class="orig-item-container" data-start-with="[start_with_class]">
    [wpv-post-body view_template="name of the content template"]
  </div>
</wpv-loop>
..........

5. The last step is to include some custom script in the view's "JS editor" ( screenshot: hidden link ), that cycles through all the results available in the div with id "orig-container" and moves them inside the empty div with id "new-container", grouped by the starting character:


jQuery(document).ready(function( $ ) {
  var arr = [];
  i = 0;
  $('#orig-container .orig-item-container').each(function()
  {
    var currentStartWith = $(this).attr('data-start-with');
    if(jQuery.inArray(currentStartWith, arr) == -1) {
      arr[i++] = currentStartWith;
      $('#new-container').append( '<div class="new-item-container" data-start-with="'+currentStartWith+'"><h3>'+currentStartWith+'</h3></div>' );
    }
    $('#new-container .new-item-container[data-start-with="'+currentStartWith+'"]').append($(this).html());
    $(this).remove();
  });
});

I hope this helps and please let me know if any step or point is not clear.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2326175

Hi again Waqar.
You are a genious. It works - in one column.
Ive tried to make it work with 3 columns but can't get it to work with this "rewrite". Maybe you can tell me what I'm doing wrong?

This in put in the field "Loops Editor":

[wpv-layout-start]
[wpv-items-found]
<div id="new-container"></div>
<div id="orig-container" style="display:none;">
<!-- wpv-loop-start -->
<wpv-loop wrap="3" pad="true">
[wpv-item index=1]
<div class="row ">
<div class="col-md-4">[wpv-post-body view_template="loop-item-in-medlemsliste-first-letter-1"]</div>
[wpv-item index=2]
<div class="col-md-4">[wpv-post-body view_template="loop-item-in-medlemsliste-first-letter-1"]</div>
[wpv-item index=3]
<div class="col-md-4">[wpv-post-body view_template="loop-item-in-medlemsliste-first-letter-1"]</div>
</div>
[wpv-item index=pad]
<div class="col-md-4"></div>
[wpv-item index=pad-last]
<div class="col-md-4"></div>
</div>
</wpv-loop>
<!-- wpv-loop-end -->
</div>
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

This is put in the field "Templates for this view":

wpv-post-title]
[types field="ekf-address"][/types]
[types field="ekf-zip"][/types]
[types field="ekf-city"][/types]
[types field="ekf-phone"][/types]

Kind regards Kristen

#2326211

Hi again again Waqar.

I hope you have patience with my lack of skills.

I now have found a few bugs and removed them from the "Loop Editor:

[wpv-layout-start]
[wpv-items-found]
<div id="new-container"></div>
<div id="orig-container" style="display:none;">
<!-- wpv-loop-start -->
<ol class="wpv-loop js-wpv-loop">
<wpv-loop>
<div class="orig-item-container" data-start-with="[start_with_class]">
<div class="row">
<div class="col-md-4">[wpv-post-body view_template="loop-item-in-medlemsliste-first-letter-1"]</div>
[wpv-item index=2]
<div class="col-md-4">[wpv-post-body view_template="loop-item-in-medlemsliste-first-letter-1"]</div>
[wpv-item index=3]
<div class="col-md-4">[wpv-post-body view_template="loop-item-in-medlemsliste-first-letter-1"]</div>
</div>
[wpv-item index=pad]
<div class="col-md-4"></div>
[wpv-item index=pad-last]
<div class="col-md-4"></div>
</div>
</wpv-loop>
</ol>
<!-- wpv-loop-end -->
</div>
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

Now it show the following: hidden link

As you can see the view doesn't show in 3 colums and "First Letter" header is only shown ny the number "3".
The Views also shuffles to the left until they leave the screen.

Do you have any idea what the problem is?

Thank you in advance.

Kind regards

Kristen

#2327001

Thanks for the update and glad that it worked.

To show the results in 3 columns, you can update the content in the "Loop Editor" to:


[wpv-layout-start]
  [wpv-items-found]
  <div id="new-container"></div>
  <div id="orig-container" style="display:none;">
    <!-- wpv-loop-start -->
    <ol class="wpv-loop js-wpv-loop">
      <wpv-loop>
        <div class="orig-item-container" data-start-with="[start_with_class]">
        <div class="col-md-4">[wpv-post-body view_template="loop-item-in-medlemsliste-first-letter-1"]</div>
        </div>
      </wpv-loop>
    </ol>
    <!-- wpv-loop-end -->
  </div>
  [/wpv-items-found]
  [wpv-no-items-found]
    <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
  [/wpv-no-items-found]
[wpv-layout-end]

And the custom script will need to be updated slightly as well:


jQuery(document).ready(function( $ ) {
  var arr = [];
  i = 0;
  $('#orig-container .orig-item-container').each(function()
  {
    var currentStartWith = $(this).attr('data-start-with');
    if(jQuery.inArray(currentStartWith, arr) == -1) {
      arr[i++] = currentStartWith;
      $('#new-container').append( '<div class="new-item-container" data-start-with="'+currentStartWith+'"><div class="row"><div class="col-md-12"><h3>'+currentStartWith+'</h3></div></div></div>' );
    }
    $('#new-container .new-item-container[data-start-with="'+currentStartWith+'"] .row').append($(this).html());
    $(this).remove();
  });
});

#2327405

Hi Waqar.
You are a hero! Now I just style it a bit and it will be just like I want it.
Thank you very much.
One thing I discover now - and maybe it is a totally new ticket - is that the sorting order isn't quite correct.
Maybe the problem is that this is danish letters (Æ, Ø and Å) and they come last in the alphabet.
Is this a UTF-8 problem and do you have an idea how to get around this problem?
Thank you so much in advance.
Kristen

#2328557

To make this work for the danish letters too, you can update the code for the shortcode (step 1), slightly:


add_shortcode('start_with_class', 'start_with_class_func');
function start_with_class_func($atts) {
    return mb_strtoupper(mb_substr(get_the_title(), 0, 1));
}

#2328931
Still wrong order in alphabet.png

Hi again Waqar.
Thank you for your assistance.
I may have been blurry in my explanation so I'll try again 🙂
When I look at the sorting it says: 3 - A - Æ- B... - but it should have said 3 - A - B...Z - Æ...
I have taken a screenshot to show what I mean.
The correct order would be:
1 - 9
A - Z
Æ
Ø
Å

I hope it makes more sense right now.