Skip Navigation

[Resolved] Sort by Featured Listings

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 11 replies, has 2 voices.

Last updated by Minesh 1 year, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2629283
Excel Featured Example.png
Remove duplicates custom code.png
Hide results custom code final.png
Featured checkbox.png

Tell us what you are trying to do?

Hello,

I added a checkbox to my posts that show featured tuners. I've attached an image of the configuration. What I would like to do is sort results in my view by that custom field, allowing them to be shown first in the list. After that, I was hoping to randomize the results.

My view has custom code already that removes duplicates and hides the results until a search is performed. I've attached screenshots of the code in case it interferes with what you're trying to do. I've also attached a screenshot of an Excel example where the list is sorted, but not filtered, by a Featured column.

Is there any documentation that you are following?

This lesson mentions featured listing but doesn't tell you how to do it. I don't want to filter the results, just sort the listings with the "Featured" check first.

What is the link to your site?
hidden link

Please let me know any questions. Thank you.

#2629391

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you edit your view and then navigate to the "Ordering" section and set your view to order by your custom featured field?

Does that help?

#2629543
Search tool ordering settings.png

It does help. It looks like this will give me exactly what I want. However, it's not saving the values that I enter. I select the view, then select the changes, then select update. But when I refresh the page, the settings are back to default. The preview doesn't even show my changes.

#2629553

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2629583

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The reason why you can not change the ordering for your view because you can set the ordering for the custom fields which belongs to post type you set your view the query.

On the page:
- hidden link

Your view is set to query the intermediary post type "Platforms Tuners Intermediary Posts" and the custom field "Featured" is belongs to post type "Tuner" that is why you can not able to set order by with Featured custom field for this view.

How you want it should work?

I see only one item is set as featured:
- hidden link

When we set order by the view will display only posts where featured is set - do you want to display only featured posts with your view irrespective of what user search for but it should display only featured posts?

#2629755

it doesn't seem like any of the sort items are working. I set it Random Order and that doesn't work either. So is something else wrong?

Also, there code that can, after submit is clicked:
Look at the tuner name in the first column
Query the name to determine if it's Featured
Mark it as featured
Then move it to the top?

The featured field leaves a value in all Tuner posts. Either it's a 0 or it's check marked. So it should show all of the results. Assuming it's a 1 when it's checked, it can sort in descending value with all of the 1's at the top and the 0's at the bottom.

#2631437

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The featured field leaves a value in all Tuner posts. Either it's a 0 or it's check marked. So it should show all of the results. Assuming it's a 1 when it's checked, it can sort in descending value with all of the 1's at the top and the 0's at the bottom.
==>
As you know the value of featured belongs to posts of post type Tuner thats what I would like to clarify that you can not sort this view with the featured custom field that belongs to post type Tuner as your view is set to query the content type (post type) intermediary post type.

Let me see if I can share any workaround.

#2632097

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share what post you set as featured. Can you please share link of the featured posts.

#2633115

Should only be one listing that’s set to featured, which is MAPerformance. All of the rest of the entries have a “0” entry in that field

#2634809

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

On your custom fields group where you added the "Featured" custom field:
- hidden link

You had "Value to store" setup as "Featured" test that I changed to 1,

Then I setup the featured value for the following post and saved the post:
=> hidden link

I've adjusted the code snippet as given under:

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;
}

Can you please confirm it works as expected now.

#2636887
Featured listing post-selection results.png
Featured listing post-selection.png
Featured listing pre-selection results.png
Featured listing pre-selection.png

Hello,

It seems to partially work, but not entirely. It does look like it moves some of them up as duplicate entries. As an example, I added Original Auto as a Featured listing. However, when I do that, it duplicates the entries listed. See example pictures. Any ideas why this is? Could this be because of my other deduplication code?

#2637009

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now:
=> hidden link

I've adjusted the code snippet as given under to the "Custom Code" section with code snippet "remove-duplicates":
=> hidden link

 
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 and !in_array($tuner_id[0], $arr)) {
       
          	$all_featured[] = $post;
            $arr[] = $tuner_id[0];
          
        }else{
          if(isset($tuner_id[0]) && !in_array($tuner_id[0], $arr)){ 
     		$res[] = $post;
            $arr[] = $tuner_id[0];
          }
        }
        
        
      }
    
    
      $query->posts = array_merge($all_featured,$res);
    }else{

      $query->posts = array();
    }
    return $query;
}

Can you please confirm it works as expected now.

#2637067

It does look like this works perfectly now. Thank you for the time you spent on this!