Skip Navigation

[Resolved] Create a View that pulls in all Posts and only certain Pages by ID

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to make a query that is WHERE ((item is Post) OR (item is Page and Page ID in {12, 53, 77, 83}))?

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/create-a-view-that-pulls-in-all-posts-and-only-certain-pages-by-id/#post-1605249

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

https://developer.wordpress.org/reference/hooks/posts_where/

This support ticket is created 4 years, 8 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by Erik F. 4 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1604813

I have a Resources page on my client's site that currently lists out the titles of Posts from the blog, sorted alphabetically. This is a simple View that pulls those in. My client is now asking that I add in 4 Pages into the list as well. I thought I could just edit the View, check the box to include Pages in the mix, but now I'm not sure how I can create the query to say something like "show all Posts, and also Pages that have one of these ids". Not sure how to make a query that is WHERE ((item is Post) OR (item is Page and Page ID in {12, 53, 77, 83})). Is something like that possible, either in a single View, or as 2 different Views that are then combined into a single list in a third View? Any help you can provide would be appreciated.

#1605249

Hello,

It needs custom codes, for example, you can try with Views filter hook wpv_filter_query, add below PHP codes into your theme file "functions.php":

add_filter('wpv_filter_query', function($query, $settings, $view_id){
	if($view_id == 123){
		add_filter('posts_where', 'add_pages', 10, 2);
	}
	return $query;
}, 10, 3); 

function add_pages($where, $query){
	global $wpdb;
	$where .= " OR $wpdb->posts.ID IN (456, 789)";
	remove_filter('posts_where', 'add_pages', 10);
	return $where;
}

Please replace 123 with your post view's ID, replace 456, 789 with those specific page's IDs.

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/hooks/posts_where/

#1607313

Thank you for your help. This did exactly what I wanted. Thanks also for the links to the docs. I wasn't aware of that stuff so now I can go dig in on my own to get the full power of Toolset.