Using Elementor, how can I adjust the posts query to include pages that have a specific category?

In Elementor you can override the query being used in the Posts widget. So far I succeeded in creating a function and apply some customization. However, I can’t seem to figure out how to get pages with a specific category, but still get all posts as well.

So far I tried the following:

“`
function fetch_posts($query)
{
$query->set(‘post_type’, [‘post’, ‘page’]);
$query->set(‘post_status’, ‘publish’);
$metaQuery = (array)$query->get(‘meta_query’);
$metaQuery[] = [
‘relation’ => ‘OR’,
[
‘key’ => ‘foo’,
‘compare’ => ‘NOT EXISTS’
],
[
‘relation’ => ‘AND’,
[
‘key’ => ‘foo’,
‘compare’ => ‘EXISTS’
],
[
‘key’ => ‘foo’,
‘compare’ => ‘=’,
‘value’ => ‘1’
] ] ];
$query->set(‘meta_query’, $metaQuery);
}
add_action(‘elementor/query/fetch_posts’, ‘fetch_posts’);
“`

What I tried to do was get all posts where the ACF field foo does not exist, because I only added the ACF field to pages. And get all pages where the foo field is set to true. But I have a feeling this can be done with more ease and simplicity.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer