Skip Navigation

[Resolved] How to display CP in a view only if they're not being displayed in anohter view

This support ticket is created 4 years 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 21 replies, has 2 voices.

Last updated by Dan 4 years ago.

Assisted by: Minesh.

Author
Posts
#2121605

Dan

Hi there,

I hope you can point me in the right direction here. I need to display CP "News" in a view only if they're not currently being displayed in another view.

I have 3 views:
1. Top news, with the assigned taxonomy "Top News" and displaying only the latest "10" posts.
2. Featured news, with the assigned taxonomy "Featured News" and displaying only the latest "15" posts.
3. All news, which displays all news including Top and Featured news.

Now, what I need to do is for the posts in the first 2 views not to display in the last one "All news" if they're currently being displayed. So, once a new post is added with any of these 2 taxonomies "Top News" and "Featured News" and pushes the last one out of display, this removed one will show in the "All news" view. In other words, only to show one post at any time in any of these views.

Thanks in advance,

Dan

#2122083

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

This will require to use view's filter hook wpv_filter_query where we should first find all the posts attached to both terms "Top News" and "Featured News" and use the found post IDs and hook in with query argument post__not_in.
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

If you do not know how to do it, please share problem URL where you want to display ALL news and 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.

#2122881

Dan

My previous post got submitted incomplete by error...

Thank you for your feedback Menish. I've never configured views this way and since you offered I'm going to accept your help. I have provide you with the site access info. Under views, there are only 3 views, so it should be simple to know which one is what. The page where I have them currently being displayed is /home-news

Let me know if you need any additional information

Thanks again,

Dan

#2123029

Minesh
Supporter

Languages: English (English )

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

Can you please share details on what page/post you added the other two views that displays the filtered results based on terms "Top News" and "Featured News"?

#2123043

Dan

Sure, here's detailed info:

I have the 3 views at this page: hidden link (this will eventually be the homepage). The views are:
[wpv-view name="home-top-news"]
[wpv-view name="home-featured-news"]
[wpv-view name="home-all-news"]

The first two views are set the same:
Content Selection: Post Types: News Articles
Don't include current page in query result
Order by post date, descending
Display 10 items for Top news
Display 10 items for Featured news
Query Filter: Taxonomy filter: Select posts with taxonomy: Article placements in one of these: Top news and Featured for the other view

And that's basically it until I know how to proceed.

Then, the view [wpv-view name="home-all-news"] has: News Articles, limit to 30 items ordered by post date, descending

I hope this is what you asked for.

Thanks,

Dan

#2123135

Minesh
Supporter

Languages: English (English )

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

I've added following query filter to your ALL News view:
=> hidden link

Select posts with taxonomy:
Article placements in no one of these: Featured, Top news

Can you please confirm it work as expected:
- hidden link

#2123169

Dan

I saw your filter but this is not what I want to achieve. With your filter, it simply skips the posts with the taxonomies selected. However, my intention is to not have to uncheck the taxonomy in order to show under All News view. The taxonomy will continue to be checked, as we'll have an archive all of Top News, so, the filter itself should be based if the post is not in the top 10 Top news (or top 15 featured news) then it shows under All news.

Does that make sense? In other words, when a new top news is added the 11th one will show under All news, and so on.

#2123179

Minesh
Supporter

Languages: English (English )

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

As I can see with Top news view you set view to display 10 posts only.
As I can see with Featured news view you set view to display 15 posts only.

Do you mean that, if there are 12 top news and 17 features news, the remaining 2 top news and 2 featured news you want to display with ALL news view (having said that the ALL news view should display the other remaining top and feature news posts only?)

#2123181

Dan

Exactly. Basically any excess of the limit set for top or featured news will display under all news without having to deselect the taxonomies. Is this possible?

#2123195

Minesh
Supporter

Languages: English (English )

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

Ok got it.

I've changed the "Query Filter" as given under for "All news" view:

Taxonomy filter Delete  Edit
Select posts with taxonomy:
Article placements in one of these: Featured, Top news

And added the following code to "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_filter_query_post_process', 'func_found_top_and_featured_news', 10, 3 );
 function func_found_top_and_featured_news( $query, $view_settings, $view_id ) {
  global $top_news_ids,$featured_news_ids;
  
   if ( $view_id == 215917 and !empty( $query->posts) ) {
     	
     	foreach($query->posts as $k=>$v):
     		$top_news_ids[] = $v->ID;
     	endforeach;
   }
     
    if ($view_id == 215924 and !empty( $query->posts )) {
     foreach($query->posts as $k=>$v):
     		$featured_news_ids[] = $v->ID;
     	endforeach;
      
   }
    return $query;
}

function func_display_all_news_posts( $query_args ,$view_settings, $view_id ) {
     
  global $top_news_ids,$featured_news_ids;
     
    if ( $view_id == 215929 ) {
         
       $query_args['post__not_in'] = array_merge($top_news_ids,$featured_news_ids);
         
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_display_all_news_posts', 10, 3);

Can you please confirm it works as expected now.

#2123203

Dan

Thanks again Minesh for your assistance. This is almost there.. I think.

Under All news, I need to display all posts including those that are not under those two taxonomies. Basically display all of them by published date including top and featured posts that exceeded their limits you set in your filter. I went ahead and removed the query filter "Select posts with taxonomy: Article placements in one of these: Featured, Top news " from the all news view, but the posts that don't have a taxonomy don't show, for example, this one: hidden link

I thought by removing the query filter would display all, but this is not the case. Is this set under custom filter?

#2123221

Minesh
Supporter

Languages: English (English )

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

Yes - you should just remove the "Query Fitler" and it will display all posts but there is no way to order it by display the posts order by remaining terms first and then other posts.

#2123223

Dan

That's what I thought, so I removed the query filter and still only posts with the taxonomies show. The ones without don't show and I have the view sorted by date descending. Shouldn't the posts without taxonomies blend in with all other post?

#2123233

Minesh
Supporter

Languages: English (English )

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

Its not displaying other posts because you set to limit your view to display 50 posts.

You must have remaining top and featured news posts more than 50 and then it should display the other posts.

#2123245

Dan

Ok, I understand. So it's not possible to list them all without listing top and featured first? Just like what a normal view would display regardless of taxonomies? So, under "view all" it displays all, including the excess from the two taxonomy views.