Support

Account

Home Forums General Issues Post type = product per single product

Solved

Post type = product per single product

  • Hello,

    I have inherited a site and running into an issue.

    It seems that ACF PRO is pulling the post type is equal to product from the first product that was setup. It seems to not recognize the set featured products on each individual product page.

    Example:
    Product A has featured products set to feature ( B, C, D, E)
    front end displays products B, C, D, E correctly

    Product B has featured products set to (A, C, D, E)
    front end displays products B, C, D, E instead of A,C, D, E

    The following code is used to display the featured products on each:

    <!-- <div class="featured-product"> 
            <div class="title-featured">
                <h3>You may also like</h3>
            </div>-->
      
            <?php
                $meta_query  = WC()->query->get_meta_query();
                $tax_query   = WC()->query->get_tax_query();
                $tax_query[] = array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'name',
                    'terms'    => 'featured',
                    'operator' => 'IN',
                );
                
                $args = array(
                    'post_type'           => 'product',
                    'post_status'         => 'publish',
                    'orderby' => 'post_date',
                    'order' => 'DSC',
                    'posts_per_page'      => 1,
                    'meta_query'          => $meta_query,
                    'tax_query'           => $tax_query,
                );
                ?>
                <div class="row">
                    <?php
                    $featured_query = new WP_Query( $args );
                       
                    if ($featured_query->have_posts()) {
                            while ($featured_query->have_posts()) : 
                                $featured_query->the_post();
                                
                                $product = get_product( $featured_query->post->ID );
                                $price = $product->get_price_html();
                                 $post_objects = get_field('featured_products');
    
    			if( $post_objects ): ?> 
    <div>
    			<h1>You may also like</h1>
    				
    
    				<?php foreach( $post_objects as $post_object): ?>
    
    					<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
    					   <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_object->ID ), 'single-post-thumbnail' );  ?>
    					  
    						<div class="single-feature">
    								<a href="<?php echo get_the_permalink($post_object->ID); ?>" class="img-feature">
    								 <img src="<?php echo $image[0];?>">
    								</a>
    							<div class="product-title-price">
    								<a href="<?php echo get_the_permalink($post_object->ID); ?>">
    								<h3><?php echo get_the_title($post_object->ID); ?></h3>
    								</a>
    								<?php echo $price; ?>
    							</div>
    						</div>
    
    					</div>
    
    				<?php endforeach; 
    				 endif;
               
                        endwhile;
                    }
                    ?>
                </div>
        </div>
    
    </div>

    Any help here would be amazing!

  • The code is working exactly as you describe and it seems that is what it is meant to do.

    The first part

    
    <?php
                $meta_query  = WC()->query->get_meta_query();
                $tax_query   = WC()->query->get_tax_query();
                $tax_query[] = array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'name',
                    'terms'    => 'featured',
                    'operator' => 'IN',
                );
                
                $args = array(
                    'post_type'           => 'product',
                    'post_status'         => 'publish',
                    'orderby' => 'post_date',
                    'order' => 'DSC',
                    'posts_per_page'      => 1,
                    'meta_query'          => $meta_query,
                    'tax_query'           => $tax_query,
                );
                ?>
                <div class="row">
                    <?php
                    $featured_query = new WP_Query( $args );
    

    This is getting a 1 product that has the “visibility” taxonomy term of “feature_product” set. This will always get the same post. The the rest of the code is getting the featured product as set on this product.

    I don’t know where your code is running or in what template, but the above code should likely be removed and somewhere in the single product template you should be getting the fields from the current product being shown rather than the above. The code you posted would be useless for doing this and if that’s not what should be happening should probably all be removed.

  • Hi Jhon,

    appreciate the reply! The code above is coming from woocommerce/content-single-product.php

    Basically there is an ACF field called featured_products that is displayed on the each product page while creating/editing a product. This would be so we can select what products we want to feature for the current product.

    With your help I have resolved the issue in the code previous developer had done.

    I amended the code and left out the following:

     <!--  php
                $meta_query  = WC()->query->get_meta_query();
                $tax_query   = WC()->query->get_tax_query();
                $tax_query[] = array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'name',
                    'terms'    => 'featured',
                    'operator' => 'IN',
                );
                
                $args = array(
                    'post_type'           => 'product',
                    'post_status'         => 'publish',
                    'orderby' => 'post_date',
                    'order' => 'DSC',
                    'posts_per_page'      => 4,
                    'meta_query'          => $meta_query,
                    'tax_query'           => $tax_query,
                );
                 -->

    Then edited the code below this to reflect

    <?php
                    $post_objects = get_field( 'featured_products' );
                       
                    
    			if( $post_objects ): ?> 
    

    while adjusting the code within the divs.

    THANK YOU SO MUCH! I would never have gotten this without your suggestion (and some heavy reading in the documentation!)

  • Basically, you would delete the custom query and it’s associated loop and you would only need the code that starts with

    
    $post_objects = get_field('featured_products');
    

    and the loop associated with this field.

    This would show posts associated with the current product.

    You would keep this, but it may need some adjustments

    
    $post_objects = get_field('featured_products');
    
    			if( $post_objects ): ?> 
    <div>
    			<h1>You may also like</h1>
    				
    
    				<?php foreach( $post_objects as $post_object): ?>
    
    					<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
    					   <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_object->ID ), 'single-post-thumbnail' );  ?>
    					  
    						<div class="single-feature">
    								<a href="<?php echo get_the_permalink($post_object->ID); ?>" class="img-feature">
    								 <img src="<?php echo $image[0];?>">
    								</a>
    							<div class="product-title-price">
    								<a href="<?php echo get_the_permalink($post_object->ID); ?>">
    								<h3><?php echo get_the_title($post_object->ID); ?></h3>
    								</a>
    								<?php echo $price; ?>
    							</div>
    						</div>
    
    					</div>
    
    				<?php endforeach; 
    				 endif;
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Post type = product per single product’ is closed to new replies.