Support » Plugin: Jetpack by WordPress.com » Alphabetising related posts

  • Hello,

    I’m trying to alphabetise related posts. I’m using this code here to display related posts only from a category the pages belong to, which is output as a list:

    function jp_only_rp_in_same_category( $categories, $post_id ) {
      $category_objects = get_the_category( $post_id );
      if ( ! empty( $categories ) ) {
          $categories = array_merge( 'categories', 'category_objects' );
          return $categories;
      }
      else {
        return $category_objects;
      }
    }
    add_filter( 'jetpack_relatedposts_filter_has_terms', 'jp_only_rp_in_same_category', 10, 2 );
    

    This is how it looks:

    Screenshot of related posts

    Could you suggest whether if the best way of alphabetising this list is via PHP or Javascript/JQuery? I have tried using Javascript/JQuery with no luck.

    I used this as a base: https://codepen.io/gabrieleromanato/pen/Cfpdu

    Modified as such:

    (function( $ ) {
    	$(function() {
    		$( "span", ".jp-relatedposts-post" ).sort(function( a, b ) {
    			return $( a ).text() > $( b ).text(); 
    
    		}).appendTo( ".jp-relatedposts-post" );
    
    	});
    
    })( jQuery );

    Code is not my strength so I might have taken the wrong approach. Any help would be appreciated!

    All the best,

    Michael

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi there,

    Unfortunately code isn’t my strong suit either but from what I can gather this would need some serious hacking of Jetpack’s core files to get this to work. I’ll ask our developers on which avenue to best take, but please bear in mind that we can’t provide support for custom development work.

    Hello Iamday,

    Great to hear on your response, please do let me the best avenue to take – it’s still on my to-sort list. Perhaps in the future JetPack developers would add an ‘alphabetise’ checkbox to make this easier for others. Thanks.

    Any updates on this?

    Hi there,

    Sorry about the delay in writing back to you. Our developers did get back and they suggested that you take a look at the jetpack_relatedposts_returned_results filter to modify what’s being returned as opposed to going down the jQuery route:

    https://developer.jetpack.com/hooks/jetpack_relatedposts_returned_results/

    I hope that helps!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Alphabetising related posts’ is closed to new replies.