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.
2
Answers
I also found a soultion that will work with Advanced Custom Fields and keeps the custom order of the relationship field:
Update
Don’t use
get_posts()
function, instead use a realWP_Query
like in the following example:This time it will works, without any issue.
An alternative: To avoid this problem, you could use instead WooCommerce
[products]
shortcode as follows:Tested and works.