Skip Navigation

[Resolved] Sorting a view based on taxonomy such that first result is of active taxonomy

This support ticket is created 3 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

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 8 replies, has 2 voices.

Last updated by puneetS-3 3 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1693165

I have created a custom taxonomy named "Topic" having many terms let say A, B. C, and D. I created a CPT where I am adding posts and marking one Topic) to each post. I am making a view to display the posts for my CPT and I want to display the list of posts to a page (the page that also belongs to any of this Topic) in such a way that the first post in the list should always be from the topic in which that particular page falls.

If there is a page marked with topic A then the list of posts should have the first post that belongs to topic A and then other posts.

I tried to do this by using a filter that the topic is set by one view shortcode attribute and the shortcode attribute is set by the current post id. But it is returning only the post of that particular topic and I want to display all posts.
Please help me to get the expected output.

Thanks!

#1697745

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

It seems the easiest way to achieve this is you should use the view's filter hook: "wpv_filter_query_post_process"
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

With the above hook, you will have access to $query->posts variable (that contains the all results returned by view's query), you just need to manipulate that $query->posts result array and set the post which is having the matching term as first post in the array index of $query->posts.

#1699193

Hi Minesh,

I am not sure how do I exactly use this hook.
Can you please share some examples for my better understanding?
Your help is appreciated.
Thanks.

#1699379

Minesh
Supporter

Languages: English (English )

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

Example is given with the Doc shared:

add_filter( 'wpv_filter_query_post_process', 'prefix_modify_empty_query', 10, 3 );
 function prefix_modify_empty_query( $query, $view_settings, $view_id ) {
    if ( empty( $query->posts ) ) { // if the query found no posts
        $default_post = get_post(1); // get the post with ID equal to 1
        $query->posts = array( $default_post ); // add the default post to the posts result array
        $query->found_posts = 1; // modify the count of found posts
        $query->post_count = 1; // modify the count of displayed posts
    }
    return $query;
}

- You need to adjust the above code as per your need. If you do not know how to do it, please share problem URL and access details with your expected results.

#1701327

Here: hidden link
There is a slider on the above page with 4 slides each of the different topics. As I already marked the page with the "Stress" topic. So, I am trying to get the first slide of the slider is also of "Stress" topic which is stress recovery.
This is what I am expecting.
I don't know how can I get this with the "wpv_filter_query_post_process" hook.

#1704807

Minesh
Supporter

Languages: English (English )

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

Well - I will require the access details to review your current setup and check if its feasible to use the "wpv_filter_query_post_process" hook.

*** 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.

#1704853

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the following code using the view's filter hook: wpv_filter_query_post_process - to the "Custom Code" section of Toolset with snippet "Toolset Custom Code":
=> hidden link

add_filter( 'wpv_filter_query_post_process', 'func_change_post_order_per_tax', 10, 3 );
 function func_change_post_order_per_tax( $query, $view_settings, $view_id ) {
   
   global $post;
 if ($view_id == 9004111222088491 ) { 
   $assigned_terms = wp_get_object_terms( $post->ID,  'topics',array('fields'=>"slugs"));
                                        
   $res_arr = array();
   $arr_index = 1;
   foreach($query->posts as $k=>$v):
    $current_assigned_terms = wp_get_object_terms( $v->ID, 'topics',array('fields'=>"slugs"));
   		if($assigned_terms[0]== $current_assigned_terms[0] ){
       		 $res_arr[0]= $v;
     	}else{
     	    $res_arr[$arr_index++] = $v;
        }
   	endforeach;
    
    $query->posts = $res_arr;
         
    }
    return $query;
}

I can see it working as expected, Can you please confirm it works at your end as well 🙂

In addition to that, I see you are using outdated Toolset plugins. We always recommend running your site with the latest stable release plugin version.

*** Please make a FULL BACKUP of your database and website.***
Could you please update ALL Toolset plugins to it's latest officially released version. You can download the latest plugin release from your accounts page:
=> https://toolset.com/account/downloads/

#1704957

Minesh
Supporter

Languages: English (English )

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

Can you please confirm the solution I shared works at your end.

#1705063

Yes! it is working as expected at my end as well.
Okay. I will update all to it's latest version.
My issue is resolved now.
Thank you Minesh!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.