Support

Account

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

Solving

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();
    
    the_title();
    
    endwhile;
  • 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)

The topic ‘get ID of woo commerce product through ACF page link’ is closed to new replies.