Reordering results within paged search

This topic contains 10 replies, has 3 voices, and was last updated by  Kiran 4 years, 2 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket

Tagged: 

  • Author
    Posts
  • #528555

    Kevin Holdridge
    Full Member
    Post count: 45

    I need to reorder the 5 results selected for each page of search results (i.e. reordering *within* each subset of 5 rather than changing the overall order of the query results sent for paging).

    On v1 I achieved that by making a copy of listing-listview.php in the theme’s geodirectory subfolder and adding a function to run usort on $posts straight after ‘$posts = $wp_query->posts;’

    Can’t see how best to do that on v2 after upgrading. Would welcome any hint. Thanks.

    #528606

    Alex Rollin
    Moderator
    Post count: 27815

    The developers will check your topic and let you know any advice they have about that

    #529326

    Kevin Holdridge
    Full Member
    Post count: 45

    Thanks, Alex. Any progress?

    #529453

    Kevin Holdridge
    Full Member
    Post count: 45

    The only way I can find currently to do this is to apply the usort straight after

    $posts = $wp_query->posts;

    in /plugins/geodirectory/templates/content-archive-listing.php

    I’d prefer not to have to do this in core files. I can’t see any codex of hooks and filters for v2 yet. But I suspect there may be a way to hook into

    geodir_before_listing_post_listview ??

    #530082

    Kiran
    Moderator
    Post count: 7069

    Hi Kevin,

    Provide us custom code that you used in GDv1. Also tell us on which pages you want to customize this.

    Regards,
    Kiran

    #531255

    Kevin Holdridge
    Full Member
    Post count: 45
    This reply has been marked as private.
    #531378

    Kiran
    Moderator
    Post count: 7069

    Hi,

    Still not clear how you it implemented in GDv1 without checking GDv1 files and site.

    If khsearchranking field is added then you can use this file in sorting by using hook. Check snippet provided here to apply own sorting on search page query.
    https://wpgeodirectory.com/support/topic/search-for-2-cpts-not-working/#post-522151

    You can change featured to khsearchranking there to sort results by khsearchranking.

    Kiran

    #531394

    Kevin Holdridge
    Full Member
    Post count: 45

    Hi Kiran

    Thanks for your follow up, I appreciate it.

    The site has been upgraded to v2 (reluctantly, I have to say), so I can no longer show you it in v1 state. Our v1 implementation was based on using the same custom usort function and calling it in listing-listview.php. Unlike with v2, however, v1 let you make an override copy of such core files in your theme folder so changes were not overwritten with every update. I have already had to reinsert our changes twice after GD2 updates overwrote them. I am a big fan of GD, but this does all feel like a step backwards I am sorry to say. I like the idea of moving to hooks and filters instead of local files for customisation, but I’m not yet seeing much documentation to help us to take advantage of that in practice. And much of the change of model feels to be making stuff harder rather than easier (e.g. can’t use custom templates in different parts of the system, you have to kludge with css to show/hide variations). Not trying to be negative, just honest feedback from an average user.

    Yes, we are already using a variation of the snippet you referenced (https://wpgeodirectory.com/support/topic/search-for-2-cpts-not-working/#post-522151). That seems to work well to order the full set of results, but does not handle the additional reorder that we need at the level of individual pages of x results – which is what we have made the changes described above to achieve.

    Tks
    Kevin

    #531407

    Kiran
    Moderator
    Post count: 7069

    Thanks for the feedback.

    If you have no site running with v1 with similar feature then provide us files you modified in v1 to implement reordering. Please attach those files in zip.

    I see code you provided but i need to check at where the code was injected.

    Regards,
    Kiran

    #531417

    Kevin Holdridge
    Full Member
    Post count: 45
    This reply has been marked as private.
    #531527

    Kiran
    Moderator
    Post count: 7069

    Hi Kevin,

    Remove all core files changes if you have any & try following PHP snippet.

    
    
    function gd_snippet_reordering_posts() {
    	global $posts;
    
    	if ( ! empty( $posts ) && isset( $posts[0]->khsearchranking ) && isset( $posts[0]->distance ) ) {
    		usort( $posts, 'gd_snippet_usort_posts' );
    	}
    }
    add_action( 'geodir_before_listing_post_listview', 'gd_snippet_reordering_posts', 1 );
    
    function gd_snippet_usort_posts( $a, $b ) {
    	if ( $a->khsearchranking == $b->khsearchranking ) {
    		if ( $a->distance == $b->distance ) {
    			return 0;
    		}
    		return ( $a->distance < $b->distance ) ? -1 : 1;
    	} else  {
    		return ( $a->khsearchranking < $b->khsearchranking ) ? -1 : 1;
    	}
    }

    Regards,
    Kiran

Viewing 11 posts - 1 through 11 (of 11 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket