Home Forums Front-end Issues get ID of woo commerce product through ACF page link


get ID of woo commerce product through ACF page link

  • Hello,

    I have a product in woocommerce, it has a custom field using a page link. This works fine.

    When I am on that page, i would like to link back to the woocommerce product and also get all the woocommerce custom fields to use on this page.

    this is my first attempt at the code with no luck. I have tried linking with both the current page ID and the permalink, neither work

    $current_url = get_permalink();
    echo $post->ID;
    $args = array(
      'numberposts'	   => 1000,
      'post_type'	   => 'product',
      'meta_key'	   => 'page_link',
      'meta_value'	   => $post->ID
    $loop = new WP_Query( $args );
    echo $loop->request;
    while ( $loop->have_posts() ) : $loop->the_post();
  • What does the page link field actually return? Does it return a plain URL? And is the value the field returns identical to what get_permalink() or $post->ID return?

  • The page link field returns the URL. Yes, this is just like using get_permalink(). When selecting pages/posts. ACF stores an array of post IDs. In order to reverse query this you need to do a “LIKE” query.

    $args = array(
      'numberposts'	   => 1000,
      'post_type'	   => 'product',
      'meta_key'	   => 'page_link',
      'meta_value'	   => '"'.$post->ID.'"'.
      'meta_compare' => 'LIKE'
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.