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