• Resolved kodacollider

    (@kodacollider)


    I’ve been wrestling with this for a long time now. I’ve Googled it to death and tried tons of different hooks and actions. I can’t seem to get this working right with ALM.

    Basically, I need my ALM query to orderby title, except using natural sorting.

    For example, instead of Title 10, Title 1, Title 2…

    It would be Title 1, Title 2, Title 10…

    If I make my own WP_Query, I can achieve this easily with:

    usort($query->posts, function($a, $b) {
      return strnatcasecmp($a->post_name,$b->post_name);
    });

    Just by sticking that after the query is executed but before the loop. However when I try to use the same technique with ALM I get some really strange results.

    Is there any way I can hack this same functionality into ALM?

    Thanks so much in advance.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author Darren Cooney

    (@dcooney)

    Hi @kodacollider
    There are not any filters available to sort after the query has been run so this would be an issue.

    It’s never come up before but I could add a filter before the loop that allows for altering of the returned posts.

    It would work the same way as the following:
    https://connekthq.com/plugins/ajax-load-more/docs/filter-hooks/#alm_query_args

    Would that help?

    Thread Starter kodacollider

    (@kodacollider)

    @dcooney Yes, that would help immensely. I would appreciate that a ton!

    Plugin Author Darren Cooney

    (@dcooney)

    Ok, I’m putting out a new release next week (tues or weds) so I’ll include something then ??

    Thread Starter kodacollider

    (@kodacollider)

    That’s great, thank you very much!

    Plugin Author Darren Cooney

    (@dcooney)

    Just released a new ALM version with a filter for this.
    https://connekthq.com/plugins/ajax-load-more/docs/filter-hooks/#alm_query_after

    Something like this. Assuming your ALM ID is default
    [ajax_load_more id="default"]

    add_filter('alm_query_after_default', function($query){
    	usort($query->posts, function($a, $b) {
    	  return strnatcasecmp($a->post_name,$b->post_name);
    	});
    	return $query;
    });
    • This reply was modified 5 years, 2 months ago by Darren Cooney.
    Plugin Author Darren Cooney

    (@dcooney)

    Thread Starter kodacollider

    (@kodacollider)

    Edit: Nevermind, I’m a dummy. Got it working. Thank you!

    • This reply was modified 5 years, 2 months ago by kodacollider.
    • This reply was modified 5 years, 2 months ago by kodacollider.
    Thread Starter kodacollider

    (@kodacollider)

    Okay, here’s a strange issue I’m encountering. The usort filter is affecting both the regular loop ouput and the loop after filtering results with a search filter in different ways.

    I have a search bar alongside my ALM query:

    // ALM Archive Search
    
    jQuery(document).ready(function($) {
        var timeoutID = null;
        function archiveSearch() {
            var obj = {};
            value = '';
            var text = $("#archiveSearch");
            value += text.val();
            obj['search'] = value;
            var data = obj;      
            ajaxloadmore.filter('fade', '250', data);
        }
        $('#archiveSearch').on('input', function(e) {
            clearTimeout(timeoutID);
            timeoutID = setTimeout(archiveSearch.bind(undefined, e.target.value), 250);
        });
    	window.almFilterComplete = function(alm){
    		setTimeout(function(){ $('#archiveSearch').focus(); }, 100);
    	};
    });

    When I search with this, it returns results in the perfect order. But it’s not the same way that the posts are ordered without using the search filter.

    If I don’t add the usort filter, though, neither are correct. It’s just that the filter is affecting each in different ways, which seems strange.

    Do you have any insight about why this might be or what I could do to order the results that my ALM query is returning the same way they show up after filtering with a search term?

    • This reply was modified 5 years, 2 months ago by kodacollider.
    • This reply was modified 5 years, 2 months ago by kodacollider.
    • This reply was modified 5 years, 2 months ago by kodacollider.
    Plugin Author Darren Cooney

    (@dcooney)

    Not really, to be honest I just added the filter after the WP_Query and confirm that it is being run.

    If it’s not working, maybe the query can’t be modified in the manner in we want it too.
    $alm_query = apply_filters('alm_query_after_'. $id, $alm_query, $post_id); // ALM Core Filter Hook

    I’ll probably just leave it in, though I still don’t see how/why you would want to order after the query when you can order before it.

    Thread Starter kodacollider

    (@kodacollider)

    Well, it’s working partially.

    The query can be modified this way because it does work correctly when ALM is not in the picture. It’s just that only filtered search results with ALM show up in the expected order, the base loop is outputting them in a different order for some reason.

    I do appreciate it being there because it solved my problem partially (when using search). I’m just not sure why the regular output doesn’t match.

    I appreciate your help in adding that, either way.

    Edit: Just to address the use case, I’m not sure how I can order by natural alphanumeric before the query. WordPress doesn’t have a way to orderby that.

    • This reply was modified 5 years, 2 months ago by kodacollider.
    • This reply was modified 5 years, 2 months ago by kodacollider.
    Plugin Author Darren Cooney

    (@dcooney)

    I’m just not sure why the regular output doesn’t match.

    Yea, not sure either. I don’t know how to test the usort how you are using so kind of lost.

    What are you trying to sort on? What are your post titles you are trying to sort?

    Thread Starter kodacollider

    (@kodacollider)

    Okay. Here’s how my page looks: https://imgur.com/a/sIup7HU

    There’s an input field for search filtering above the query output with one post per row, and the posts are all game titles so a lot of them have numbers in their name.

    If I just write out my own query for these posts with the $args array and new wp_query and 'orderby' => 'name' I can get everything sorted as I’d expect:

    Title 3
    Title 4
    Title 5
    Title 10
    Title 11
    Title 12

    But when I go through ALM it’s different. Here’s my shortcode:

    [ajax_load_more id="game-index" container_type="div" css_classes="feed feed-1" post_type="game-page" custom_args="post_parent:0;" posts_per_page="25" order="ASC" orderby="name" transition="fade" scroll_distance="-150"]

    On its own, this outputs:

    Title 10
    Title 11
    Title 12
    Title 3
    Title 4
    Title 5

    If I filter the results with my search bar (ajaxloadmore.filter();) then they show up in the same order.

    Once I add this:

    add_filter('alm_query_after_game-index', function($query){
      usort($query->posts, function($a, $b) {
        return strnatcasecmp($a->post_name,$b->post_name);
      });
      return $query;
    });

    Then the shortcode output becomes:

    Title 3
    Title 10
    Title 11
    Title 12
    Title 4
    Title 5

    For some reason. However, if I use my search bar (same as above) to filter with the usort function in, I get it in the correct order:

    Title 3
    Title 4
    Title 5
    Title 10
    Title 11
    Title 12
    • This reply was modified 5 years, 2 months ago by kodacollider.
    Thread Starter kodacollider

    (@kodacollider)

    It seems like changing the posts_per_page value changes the sort order. If I change 25 to 11, I get this instead:

    Title 10
    Title 11
    Title 12
    Title 13
    Title 3
    Title 4
    Title 5
    Title 6
    Title 7
    Title 8
    Title 9
    Title 14
    Title 15

    So maybe it’s only applying the sorting within small chunks based on posts_per_page, when it should be applying to the whole list instead?

    If I change posts_per_page to 2000 I get everything sorted correctly.

    Title 3
    Title 4
    Title 5
    Title 6
    Title 7
    Title 8
    Title 9
    Title 10
    Title 11
    Title 12
    Title 13
    Title 14
    Title 15

    (But of course I can’t have 2000 posts per page, that takes way too long to load and defeats the purpose of using ALM.)

    • This reply was modified 5 years, 2 months ago by kodacollider.
    • This reply was modified 5 years, 2 months ago by kodacollider.
    • This reply was modified 5 years, 2 months ago by kodacollider.
    • This reply was modified 5 years, 2 months ago by kodacollider.
    • This reply was modified 5 years, 2 months ago by kodacollider.
    Thread Starter kodacollider

    (@kodacollider)

    It seems like it’s just pagination breaking my sorting method. Not ALM’s fault at all.

    It’s frustrating that WordPress has no way to sort posts this way on its own. I can’t be the only person in the world with post titles that contain numbers.

    I appreciate you going out of your way to add that filter, though. I’ll keep messing with it and see if I can stumble onto some sort of solution.

    Thank you again.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Need ALM query with natural (alphanumeric) ordering of post titles’ is closed to new replies.