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
- Woocommerce: remove total sum from new order’s email [closed]
- Custom query vars filters problem with pagination
- Custom post type permalinks do not appear using the link functions
- Include js script ONLY on woocommerce product category pages? [closed]
- WordPress meta_query >= &
- extract serialized array to use for wp-query
- Order BY Most Liked And Published Between Previous Week Monday And Next Week Monday
- Convert Featured products loop into Recent products loop?
- fetch woocommerce product with custom taxonomy
- Modify main query before it run without pre_get_post
- Where to copy woocommerce files to in my custom theme to avoid editing the core plugin?
- Sort posts in a specific way
- Alter Woocommerce product archive structure
- Do not duplicate posts with multiple categories in multiple loops
- Have Woocommerce show product price if id, when not logged in
- Infinite Loop – WP_Query
- How can I split my query result in 2 arrays?
- After disabling WPML multi currency, the currency is still handled by the WPML
- custom post type with role Vendor
- How do i wrap woocomerce attribute in list?
- Removing files programmatically
- array_rand not working correctly?
- WooCommerce: Display product categories to make IF statement
- 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
- WordPress PHP syntax doesn’t seem to be working correctly
- WP_Query() load selected post
- echo var into wp_query
- How to get post category list as select in front-end?
- Querying for specific tags
- Order a WP_Query by meta value where the value is an array
- Woocommerce custom Plugin in wordpress [closed]
- How to get thumbnail with pure PHP in a WordPress database?
- Limit tags shown in post
- WooCommerce “Checkout Now” button [closed]
- How do I check if there is a tag in the title
- Why my query does not run with prepare
- Custom Taxonomy Select Menu: Setting default option value?
- How to extract information from a wp_query result?
- mySQL queries are executed twice on wordpress website
- posts_nav_link pulling in multiple post types
- Displaying Event within two given time frames
- List of Events with Multiple Dates: Only NEXT Date
- Echo array value
- Remove Post if Advanced Custom Field is checked to fix paging
- Undefined variable: row?
- Toolbar Hidden in a Virtual Page
- displaying a fall back query if there’s nothing in the post-type category
- WordPress SQL JOIN query
- Using multiple variables to assign categories to an array
- Search.php gets metadata from first post
- Change Query Arguments (filter) with jQuery/Ajax or PHP?
- the_post(); prints out style text into my HTML?
- WP_Query and help with the loop for magazine front page
- Modifying WP URL handing code?
- Display latest post from WordPress Featured Category that is also in X,Y,or Z categories
- Query pulling a single post per month
- WordPress Custom Query: Combining Two Functions
- SQL error with custom query
- How to separate product payment and shipping payment in WooCommerce checkout? [closed]