Skip Navigation

[Resolved] Sort by Featured Listings – round 2

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 16 replies, has 2 voices.

Last updated by Nigel 1 year, 2 months ago.

Assisted by: Nigel.

Author
Posts
#2641149
2 - Featured.png
1 - Pre-featured.png
3 - Post Featured.png

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.

#2641415

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

I'll take a look at this, but I'll need to check some details on your site, if I could please get current credentials from you (I've set a private reply).

This code should do 3 things, right?

Hide results until a filter is applied.
Remove duplicate results.
Boost featured tuners to the top of the results.

#2641513

Also, yes, the 3 parameters are correct.

#2641857

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I reworked the code and tested it on your site and it appears to be working now, if you'd like to confirm.

/**
 * Modify output of View "Tuner Search"
 * 
 * 1. no initial results until a filter is applied
 * 2. avoid duplicates
 * 3. promote featured results to the top
 */

add_filter('wpv_filter_query_post_process', 'remove_duplicates_query', 101, 3);

function remove_duplicates_query($query_results, $view_settings, $view_id)
{
    $viewid = 300; 
    $relationship_slug = 'platform-tuner';

    if ( $view_id != $viewid )
        return $query_results;

    // has a filter been applied?
    if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) {
        $query_results->posts = array();
        $query_results->post_count = 0;
        $query_results->found_posts = 0;

        return $query_results;
    }
    
    // iterate over results to remove duplicates and to promote featured
    $tuners = [];
    $featured = [];

    $posts = $query_results->posts;
    foreach ($posts as $key => $post) {

        $tuner_ids = toolset_get_related_posts( 
            $post->ID, 
            $relationship_slug,
            [
                'query_by_role' => 'intermediary',
                'role_to_return' => 'child',
                'return' => 'post_id'            
            ] 
        );
        $tuner_id = $tuner_ids[0];

        if ( in_array( $tuner_id, $tuners ) ) {
            // duplicate, discard
            unset( $query_results->posts[$key] );
            $query_results->post_count --;
            $query_results->found_posts --;
        } else {
            $tuners[] = $tuner_id;
            // unique (so far), is it featured?
            $is_featured = get_post_meta($tuner_id, 'wpcf-featured', true);
            
            if ( $is_featured ){
                $featured[] = $post;
                // remove from array of results, to be added back later at the top
                unset( $query_results->posts[$key] );
            }
        }
    }

    // any featured to add back at top?
    if ( $featured ){
        $posts = array_merge( $featured, $query_results->posts );
        $query_results->posts = $posts;
    } else {
        $posts = array_values( $query_results->posts );
        $query_results->posts = $posts;
    }

    return $query_results;
}
#2641927
Search tool no search.png
Search tool after search.png

Currently it's not showing any results at all, no matter the search query. So I believe something else is broken. Screenshot attached. Please advise.

#2642073

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, I'd tested it with some other search where it worked okay (if there was a featured tuner in the results), but have spotted the problem with other searches and it should be fixed now.

#2642173

Looks like that works correctly now. Thank you. However, I noticed that there's a little note that wasn't there before. Right now, when the results are hidden, the box says "No items found". What I would like it to say is "Please select your car from the dropdown above". I changed what I thought was the setting, but it doesn't seem to be taking effect.

#2642229

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

That's odd, it won't save any changes to that setting.

I temporarily disabled all plugins except Blocks and Types and tried again and that was still the case, same with changing theme.

I'm not sure why it won't accept changing that setting.

Bear with me and I'll see if there's a way to update the text in the database directly.

I'll need to look into how it is stored to see if it can be done safely. It will likely be Monday now before I can do that.

#2642651

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, I can go ahead and do that, but I'd prefer it if you had a current backup before I make any changes on your live site, if you could just arrange that first please and then let me know, and I'll go ahead and make the change.

#2642915

I have taken a backup. Let me know when you’re finished so I can test. Thank you

#2643011

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hmmm. Can you please restore the backup.

I have a copy of your site installed locally where I made the very minor changes I have now done on your live site.

It worked fine on my local copy, and it superficially appears to have worked on your live site, but the search doesn't behave as expected.

If I select Ford as the Make, I expect the Model dropdown to unlock and be populated with Ford models, but that's not happening.

I'd like to reset to see if that was working correctly before.

I was adding a small code snippet to update the No items found text directly in the database as a workaround to problems saving it within the UI, but that may point to an underlying issue I need to investigate further.

#2643193

Backup is restored. Thanks.

#2643595

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Thanks for that.

Looking more closely at your site the news isn't great.

When you create a View block there are two corresponding entries created in wp_posts with data about that View (one is effectively the real View, and the second is one that is used for generating the preview of the View in the back end while using the block editor).

The first View has a status of published, and the second has a status of draft.

On your site the second View is missing from the database altogether, which is why when editing the page with the View and trying to save changes, the changes are not kept when you reload the page.

The existing View still displays on the front end, but it is impossible to edit the View any longer. To have an editable View it would be necessary to delete the existing View and create a new one.

The handful of times I have come across this before has been when a "cleanup" operation (e.g. with a database cleaner plugin) has removed draft posts directly from the database.

If you create a new View to replace the broken View you will need to modify the code snippets you are using so that they target the new View.

#2645143

Hello,

Well, I recreated my form as best as I can. Doesn't seem to take the custom code even though I changed the code to match the new View ID. I guess it's ready for your magic.

#2645245

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I went in and updated the No items found text and added placeholder text for the tuning categories dropdown, and when I test the search on the front end it appears to be working correctly (no duplicates).

If you find that it isn't can you give me a specific example of a search that is producing the wrong results?