php - WooCommerce products: Wrong product permalink in custom loop
Get the solution ↓↓↓I'm using a custom loop to show a selection of products. The loop works fine and shows the products with the correct title, image and the other stuff. But the permalink is the URL of the current page.
If I add$post
inside the permalink, everything works fine:get_permalink($post)
Here's my current code:
<?php $featured_posts = $products_select; if( $products_select ): ?>
<div>
<ul>
<?php foreach( $featured_posts as $post ): setup_postdata($post); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
</div>
I checked the content of$products_select
and saw, that there is no link stored.
Could that be the problem? Is there any way to correct the permalink?
The variable$products_select
is the output of a custom field based on relationship field from Advanced Custom Fields. It is stored as post object.
Answer
Solution:
Update
Don't useget_posts()
function, instead use a realWP_Query
like in the following example:
<?php
$loop = new WP_Query( array(
'post_status' => 'publish',
'post_type' => 'product',
'posts_per_page' => 10,
) );
?>
<div>
<ul><?php
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
echo '<pre>ID: '.get_the_id().' | Title: '. get_the_title() . ' | Link: ' . get_permalink() . '</pre>';
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
endif;?>
</ul>
</div>
This time it will works, without any issue.
An alternative: To avoid this problem, you could use instead WooCommerce[products]
shortcode as follows:
<?php
$featured_posts = $products_select; if( $products_select ):
// get a comma separated string of product Ids
$post_ids = implode( ',', wp_list_pluck( $featured_posts, 'ID' ) );
// Use [products] shortcode with the comma separated string of product Ids
echo do_shortcode("[products ids='$post_ids']" ); ?>
</div>
Tested and works.
Answer
Solution:
I also found a soultion that will work with Advanced Custom Fields and keeps the custom order of the relationship field:
<?php
$args = array(
'post_type' => 'product',
'post__in' => $products_select,
'posts_per_page' => 4,
'orderby' => 'post__in'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : ?>
<div>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
</ul>
</div>
<?php endif; wp_reset_postdata(); ?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: property [id] does not exist on this collection instance.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.