Reordering results within paged search
This topic contains 10 replies, has 3 voices, and was last updated by Kiran 4 years, 9 months ago.
We have moved to a support ticketing system and our forums are now closed.
Open Support TicketTagged: search
-
AuthorPosts
-
February 3, 2020 at 5:18 pm #528555
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.
February 3, 2020 at 10:02 pm #528606The developers will check your topic and let you know any advice they have about that
February 7, 2020 at 4:14 pm #529326Thanks, Alex. Any progress?
February 8, 2020 at 1:42 pm #529453The 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 ??
February 13, 2020 at 5:39 am #530082Hi Kevin,
Provide us custom code that you used in GDv1. Also tell us on which pages you want to customize this.
Regards,
KiranFebruary 19, 2020 at 6:46 pm #531255This reply has been marked as private.February 20, 2020 at 10:10 am #531378Hi,
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-522151You can change featured to khsearchranking there to sort results by khsearchranking.
Kiran
February 20, 2020 at 11:21 am #531394Hi 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
KevinFebruary 20, 2020 at 1:10 pm #531407Thanks 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,
KiranFebruary 20, 2020 at 2:08 pm #531417This reply has been marked as private.February 21, 2020 at 7:13 am #531527Hi 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 -
AuthorPosts
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket