Load ACF field with file link

Hey L&L-Team,

I have added an ACF field to products in WooCommerce to store PDFs for reading samples. This works without any problems. I load the file link via L&L on the product pages and then build a shortcode for viewing the reading sample.

Now I would like to load the link to the latest reading sample on a normal page. So I would have to start a loop, isolate the newest product and then read the ACF field? Or am I wrong?

Unfortunately I don’t get any result. What is the reason for this? Is it because the field is on a product page? Do you have a solution for me? That would be great!

Thank you very much and best regards,

You nailed it; if you’re not on the page where the data is, you need to create a query (using the Loop tag) to tell L&L where to grab the data.

Gonna need more info to help with this. Might be helpful to show your template here so that we can get a better sense of what you’re trying to do.

When you say you want to show the “latest reading sample,” how are you trying to evaluate which is the latest? Would that involve simply sorting your WooCommerce product by publish date (or last modified date) and then showing the PDF associated with that product? Or would it involve querying all the PDFs you’ve uploaded across your whole site and displaying the latest one? That’ll impact whether your loop should be querying your Woo products or your attachments (media library).

Hi Benjamin, thanks for letting me know that I am on the right track. The solution I am thinking of is just sorting the WooCommerce products of a certain category by publish date and then showing the PDF associated with that product (linked with an ACF file field). Something like:

<Loop type=product category=magazine orderby=date order=desc count=1>
    <Field acf_file=pdf field=url />

But I get no result …

Huh, odd, at first glance your template seems fine. The way I generally troubleshoot L&L stuff is that I gradually simplify my template until it works, and then I rebuild it to identify which part doesn’t seem to be working.

In your case, I’d start by checking if you can display any fields from your products, like <Field title />. If that still doesn’t work, then try simplifying your query back down to something basic like

<Loop type=product>
  <Field title />

Let me know if you can identify what specific part doesn’t seem to be working for you.

Hey Benjamin, I already tested some stuff and thought I couldn’t loop products but now I learnt that it is possible. That is a new feature or am I wrong?

The problem in my code is the filtering by category. This version works, but the category is missing (see above):

<Loop type=product orderby=date order=desc>
    <Field acf_file=pdf field=url />

Can I somehow address only products of a specific category?

Oh right, I hadn’t spotted that in your earlier post but that does make sense. The category attribute is specifically for defining a post category. WooCommerce doesn’t use that taxonomy, it uses its own post types and taxonomies.

So your approach here should instead be to use the post loop’s taxonomy and terms query parameters. Should look like this:

<Loop type=product taxonomy=product_cat terms=magazine orderby=date order=desc count=1>
    <Field acf_file=pdf field=url />

Let me know if that works for you :slight_smile:

Nah, L&L is able to loop through any post type on a WordPress site and since WooCommerce’s products are just posts associated with a product post type, L&L has always been able to loop through those. Other parts of WooCommerce exist on a custom database table instead of just being a custom post type so L&L can’t loop through those for now. In instances where plugins use custom tables, we need to explicitly build an integration to make it easy to use that data, but that’s not the case for WooCOmmerce products.

That is good to know. Thank you! Works as expected.

Short question in additon: Can I somehow check for the filled field? Something like that:

<Loop type=product taxonomy=product_cat terms=magazine orderby=date order=desc count=1>
  <If field="pdf" exists>
    <Field acf_file=pdf field=url />
  <Else />
    Loop the next product

In this case I can always show a reading sample even if the latest magazine has no pdf attached yet (it would show the sample of the earlier issue).

What do you think? Would I have to place a Loop after “Else” that skips the first matching product? Or is there a better way?

Yup, that’s definitely possible. The way you’ve opted to do it with conditional logic is one way to do it, but a better way to do it is to filter your Loop tag to only query items that have that field filled out. There are three ways to approach this that differ based on how flexible and performant they are, so you can read up on the different ways to filter your loop here.

You’ll have to play around with the different options there to see what the actual value of that ACF field is when it’s empty. For example, you might try custom_field="price" custom_field_compare="not" custom_field_value="" or if that doesn’t work, maybe field="pdf" field_compare="exists".

Hope that points you in the right direction!

1 Like

That did the trick: field="pdf" field_compare="exists"

Thank you!

1 Like