Hello,
Recently I was able to get help with some sort by featured listings code. I was helped on this thread:
https://toolset.com/forums/topic/sort-by-featured-listings/
Short summary: I have a custom search tool on my website based on a many to many relationship. I was able to get some custom code added to:
Remove duplicate listings
Hide listings until a search has been initiated
Move any listing labeled as "featured" to the top of the list
Unfortunately, something happened between then and now as I'm having issues with the associated code. After the last ticket was resolved, making a listing a "Featured Listing" just moved it to the top. What it's doing now is creating duplicates of the listing. Once selected, every relationship a tuner has allows a duplicate entry in the list. I was wondering if someone could take a look at the code and point me in the right direction. The goal would be to have one entry whether the listing is featured or not.
I've attached labeled pictures.
1) Prior to selecting featured
2) Selecting "Featured"
3) Post-selecting "Featured"
Here is the custom code:
<?php
/**
* Remove duplicates results
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
//If the query does not find any posts, return a specific post with ID equal to 1
add_filter( 'wpv_filter_query_post_process', 'remove_duplicates_query', 999, 3 );
function remove_duplicates_query( $query, $view_settings, $view_id ) {
$relationship_slug = 'platform-tuner'; // here replace platform-tuner with many-to-many relationship slug
$viewid = 300; // here replace 105 with the view ID of your website
$all_featured = array();
if ( $view_id == $viewid && !empty( $query->posts ) and isset($_GET['wpv_view_count']) ) {
$res = array();
$arr = array();
foreach($query->posts as $key => $post){
$post_id = $post->ID; //intermediatory post ID
$tuner_id = toolset_get_related_posts($post_id, $relationship_slug, [
'query_by_role' => 'intermediary',
'role_to_return' => 'child',
'return' => 'post_id'
]);
$is_featured = get_post_meta($tuner_id[0],'wpcf-featured',true);
if($is_featured) {
$all_featured[] = $post;
$arr[] = $tuner_id[0];
}
if(isset($tuner_id[0]) && !in_array($tuner_id[0], $arr)){
$arr[] = $tuner_id[0];
$res[] = $post;
}
}
$query->posts = array_merge($all_featured,$res);
}else{
$query->posts = array();
}
return $query;
}
Thank you for your time.