WooCommerce has a built in function that will do this for you called wc_get_related_products()
.
That will return an array of product IDs which you can then use in a proper custom loop. I recommend you avoid using WP_Query()
and get_posts()
. See here for an article I wrote on the right way to create a custom WooCommerce loop: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
Related Posts:
- How to create a WP_Query to search the Title or Tag?
- How to display SQL query that ran in WC_Order_Query?
- Query multiple custom post types in single loop
- retrieve thumbnail from post ID of best selling product in category
- WP_Query | Help me create a search term with an ‘OR’ relation?
- Using Cookie Data For WP_Query Loop
- How to get to a specific position in wp_query?
- Why is variable not working on custom sql query using wpdb?
- How can I use wp_query to show all product data using just the products ID?
- Get stock by custom meta field on all Woocommerce variable products
- Create WP_Query to search for posts by their categories or their parent/child categories
- WP Query with custom Shortcode
- Display a specific category of products in shop page and disable code for specific actions
- WordPress default Search function inconsistent in WooCommerce Product Titles
- unable to use ‘new WP_Query’ in AJAX call
- How to add div blocks after certain set of post
- How to overwrite orderby with add_query_var
- WP_Query | IF within $args array | Help me only add specific arguments if the argument is not blank
- How to hide posts of a specific custom category in WordPress?
- WP_Query multiple post results
- WordPress WP_Query without query GET parameters
- How can I modify this code to make the search box include tags and meta
- Output product category link from WP_Query
- How to work Woocommerce pagination inside shortcode?
- WP_Query for woocommerce products with a pattern as a post_title
- How do I make these combination select filters work when only one dropdown is set? They work in WordPress 5.8.2 but don’t in 5.8.3
- get_filtered_term_product_counts – Get product terms if any products
- WP_Query To Display Product Of Brand On Taxonomy Page
- wordpress query returning unexpected results
- Woocommerce Related Product by 2 taxonomy
- Query doesn’t display text data with apostrophes
- wp_query beginner
- How can I display a query in a page?
- WooCommerce – Print Processing orders [closed]
- Does wp_query and query_posts affect website performance? [duplicate]
- Ajax Load More or View More functionality for woocommerce category layout by template overriding
- Woocommerce – Shipping tax class based on cart items not using the highest tax available
- wp_query on search results page showing all results every time
- Show post like this image in my newssite [closed]
- Three different queries on same page
- How to correctly add Javascript in functions.php
- Woocommerce: How to override core functions in functions.php?
- How to prevent those PHP variables from being cached on WordPress?
- Setting All Drafts to ‘exclude-from-catalog’ in WooCommerce
- Make the first item as default on Woocommerce product category items list
- WordPress custom slug (endpoint) and compare all links
- wp_query with cat not working
- Compare post-IDs within WP_Query? (Less than / Greater than)
- dividing sub-categories by parent category – WordPress
- Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?
- Tax query in pre_get_posts not working
- how to fix error “called incorrectly, should not be accessed directly”?
- How to auto fetch customer details in Woocommerce in checkout page
- get_posts() and WP_query limits ‘AND’ conditions to a maximum of 6 for meta value queries in WordPress
- Validation algorithm in checkout field
- Fatal error: Call to a member function query() on a non-object
- Trouble inputting variable into WP query
- Array to string conversion on array_map
- Target post_date_gmt if it’s empty or not set
- Remove page title in product page using a function
- woocommerce specific quantities for product
- Passing an array into WP_Query as a variable
- Custom query vars filters problem with pagination
- WordPress meta_query >= &
- WordPress site with embedded menu – gives You don’t have permission to access /wp-content/themes/xxx/taxonomy-redirect.php on this server
- Dividing the loop to style post differently
- Remove Post if Advanced Custom Field is checked to fix paging
- How to separate product payment and shipping payment in WooCommerce checkout? [closed]
- Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
- WP_Query: How to get results from both meta_key options?
- Passing in MySQL prepare statement parameter separately throwing error
- Load more posts using AJAX based on posts inside WP_Query
- Undefined array key 0 wp-includes/capabilities.php on line 76
- How do I know if the user is a customer?
- array wordpress when get is null
- Custom search query on WordPress page not working
- I want to fusion the product columns in email table order
- posts_per_page displays only 2 posts instead of 4 posts
- WooCommerce is_product_category() not working
- Adding new divs with custom classes to products on WooCommerce shop page [closed]
- Paged WP_Query is quitting on page 3
- Adding product permalink on admin order page throwing error
- Get Child of Child Pages in custom Menu
- Why does a header location on admin_head remove the query var I’m setting in the location?
- How to get total gross revenue for current month in the woocommerce dashboard status widget
- Get all woocommerce product names with price and show using core php
- How can i put php code on the wordpress page?
- jQuery doesn’t work in my php snippet
- Use WP_Query Data In Cookie
- Query seems to be duplicated
- How to add custom checkout field in user details mail template
- Keeping the previous get value and add another value when submitted
- Display Sale Price Before Regular Price (WooCommerce)
- How to insert a variable into a query parameter?
- Setting WooCommerce currency programmatically
- Grabbing value of input field inside of array
- How to set variable to specific field when querying
- Variable ++ in query loop
- Count custom posts type and filter by tag
- AJAX WP_Query’s order and orderby parameters not working