Support

Account

Home Forums Front-end Issues Reverse query with relationship field and Woocommerce

Solving

Reverse query with relationship field and Woocommerce

  • Hi,

    I have been able to make a reverse query like the exemple here:
    https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    The problem is, I can’t get any meta from a woocommerce product, like the price or rating.

    Here is the code:

    <?php 
    /*
    *  Query posts for a relationship value.
    *  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
    */
    $doctors = get_posts(array(
        'post_type' => 'product',
        'meta_query' => array(
            array(
                'key' => 'actresses', // name of custom field
                'value' => get_the_ID(), // matches exaclty "123", not just 123. This prevents a match for "1234"
                'compare' => 'LIKE'
            )
        )
    ));
    ?>
    <?php if( $doctors ): ?>
        <?php foreach( $doctors as $doctor ): ?>
            <article class="article-video">
                <a href="<?php echo get_permalink( $doctor->ID ); ?>">
                    <?php wc_get_template_part( 'content', 'product', $doctor->ID ); ?>
                    <?php echo get_the_title( $doctor->ID ); ?>
                    <?php echo get_the_post_thumbnail( $doctor->ID, 'small_galery' ); ?>
                    <!-- These two don't show up -->
                    <?php echo get_rating_html( $doctor->ID ); ?>
                    <?php echo get_the_price_including_tax( $doctor->ID ); ?>
                </a>
            </article>
        <?php endforeach; ?>
    <?php endif; ?>

    The problem is here:

    <?php echo get_rating_html( $doctor->ID ); ?>
    <?php echo get_the_price_including_tax( $doctor->ID ); ?>

    But I can’t figure it out.
    Any ideas?

  • Is the function get_rating_html() the same as wc_get_rating_html()? Is the other call another wc_ function? I can’t find anything on the second function.

    Can you point me to the documentation for these functions?

  • I foud it in woocommer/loop/rating.php:

    <?php if ( $rating_html = $product->get_rating_html() ) : ?>
    	<?php echo $rating_html; ?>
    <?php endif; ?>

    Here is an article about it:
    https://www.gavick.com/blog/woocommerce-product-layout

  • There isn’t really much information there on what the function does, but skimming over that link it appears that you’re missing some steps

    
    $product = get_product($doctor->ID);
    

    after doing that, if I’m reading the page right, then you can call

    
    $product->get_price_html();
    

    But you need to find out how these functions work and for that you should be looking at WC documentation or getting help from WC.

  • Thanks for your reply. It does’nt work but I manage to get these videos an other way.
    Anyway, if someone solve this problem it could be interesting 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Reverse query with relationship field and Woocommerce’ is closed to new replies.