I believe that if all your products have the custom meta field show_on_front_page
with some containing '1'
and some containing '0'
or some such, AND your regular posts do not have that field, then something like below should work:
//...
$query->set('post_type', array('post', 'product'));
//add this
$meta_key = 'show_on_front_page';
$query->set('meta_query',
array(
'relation' => 'OR',
array(
'key' => $meta_key,
'value' => '1',
'compare' => '='
),
array(
'key' => $meta_key,
'compare' => 'NOT EXISTS'
)
)
);
//...
Caveat: If some of the products don’t have the meta field set to anything they will show as well.
See https://developer.wordpress.org/reference/classes/wp_meta_query/
Related Posts:
- Pagination not working with custom loop
- How to use a custom post type as front page?
- Query custom post type by custom field
- How to solve suspected memory issue in custom WordPress loop?
- WP_Query on custom post type not displaying, multiple loops & get_template_part
- Using new WP_Query in shortcode in a custom field causes the main post content to not display
- Comparing timestamps in meta query doesn’t work
- Display two post types ordered by two custom fields
- WP insert post PHP function dynamically generated Custom Fields
- Get_post_meta() won’t return value
- Having trouble with custom date field for CPT query (WordPress)
- Display custom post on home page based on a metabox selection
- Sort posts based on multiple custom fields
- Conditional to modify query results
- Loop on front-page.php
- wp_query to find posts by year and month
- Create a random unique 6 digit number as custom field for custom post type
- query posts and custom post type with meta key
- How to insert content from another Custom Post type into Post?
- WP_Query orderby modified to include custom meta changes
- Am unable to reset a query properly
- Two near-identical custom field types – one works, the other doesn’t . What can cause this?
- Meta Key Value in current-user-only loop
- Calculate all custom field values in the post loop
- Get next and previous 3 posts in a term in single post page
- What is the recommended / best way to do this: Event calendar post/type in a block-based theme?
- Querying Two Custom Post Types with OR Not Working
- Store CPT ‘Reviews’ average ratings to a WordPress DB table or to a DB custom table?
- First custom field value (out of several) displayed twice after query
- Listing custom post types on archive page with array
- hide specific div on single.php [closed]
- Having Issue on Ordering CPT by Custom Field In Custom WP Query
- How do you output an unknown number of images in a custom post type with desired markup?
- Get data from PHP to JavaScript to set position of each post on front page
- Display a post from custom post type only if all the selected taxonomies and custom field value matches the record
- Custom Query: If One Post Object Field Value Is The Same As Another
- Custom Post Type not showing in main loop
- Get post content from another section in custom single page
- Filtering custom posts by custom fields (ACF) [closed]
- Displaying information from custom field on custom post type
- Custom Post type showing up in loop, regular posts are not showing up
- Get post-meta value of all custom-posts – lowest to highest year-count?
- Custom PT, Taxonomys and wordpress query
- Displaying Posts Using a Custom Query with a Custom Field and a term_id
- How do I list a custom field and custom taxonomies for each result in a loop?
- How can i loop through custom post type according to custom meta field?
- Changing WordPress sort order for returned child pages
- Add custom post type to query
- How to sort WP_Query by a custom text field (written as a date dd/mm/yyyy)
- Update query based on CPT / Post Type, show all sorted by date
- WP Query from two Custom Post type fields as statement
- Using OR relation in meta_query to check for a value before sorting by another
- Shortcode with loop stacks output
- when looping through custom post data not appearing
- How to avoid duplicate posts queried from custom meta?
- Meta query and compare “!=” not working as expected
- How do I query with multiple custom fields orderby in same column?
- Custom meta fields not showing up in WP_Response Object via custom endpoint
- Adding a Section for Visitors
- How can I group by Taxonomy on Custom Post Type while filtering out based on custom Meta
- sorting in wp query based on custom field value
- Custom taxonomy template for custom fields loop [closed]
- Sort loop by custom field from different post type
- How do I display specific custom posts, and how do I edit a post’s singular page?
- WordPress query in which condition uses custom field
- WordPress loop add heading before first of type
- How to make a shortcode for my WP_Query Loop? [duplicate]
- Error in WP Query. If variable is empty it is displaying previous post value
- WordPress loop, show only one post per custom field
- Show first posts with custom field not empty and order all by title
- Is it possible to add query parameters on the archive page?
- How to get past and upcoming post by defining date in custom field?
- $post breaking container loop
- Multiple Queries and Loops within CPT Archive
- Order by custom field attribute
- Get result from Custom Field in Custom Post type
- Pagination not working with custom loop
- If i have custom post type with 5 custom fields do i have to create a new loop to reference each one?
- Filter CPT based on meta box value using Flexible Posts widget?
- Pagination not working with custom loop
- Pagination not working with custom loop
- Pagination not working with custom loop
- Listing custom post type items from a couple of custom taxonomies
- Pagination not working with custom loop
- loop through custom post-type with two meta_keys
- How to grab data (titles, thumbnails and custom fields) from multiple posts to populate a new array efficiently?
- Filter Custom post type by another Custom post type
- How do I filter a custom post type loop by a field?
- Get Posts ordered by a date custom meta field
- Custom fields in Permalinks?
- How to debug a blank page?
- How to query 2 custom post types that need to share a slug?
- Pull in taxonomy field of custom post type in new query
- Query for specific taxonomy that executes a particular loop depending on volume of posts?
- How to Enable Ascending or Descending In WordPress Default Built-in Loop
- Fetch data from two custom post types and create multidimensional array for output to html table
- Query filter by value in meta_value array
- Combine multiple separate lists into one
- Custom Field to post_title
- custom fields not showing before get_header