Support

Account

Home Forums General Issues Displaying post_object content within shortcode

Solving

Displaying post_object content within shortcode

  • We have two post types, let’s call them products and brands. The products have a post_object field which selects which brand the product belongs to.

    There is a custom shortcode that outputs lists of products and we want to display the logo from the brand that has been selected in the post object field alongside the product.

    The relevant part of the shortcode is as follows:

    $brand = get_field('brand');
    if( $brand ) : 
    $ret .= '<img src="' . get_field('square_logo', $brand->ID) . '" />';
    endif;

    This works fine but I just wanted to check there wasn’t anything I was missing that could cause problems or if there was a better way to do it?

  • I don’t see anything missing. But your topic says shortcode and I don’t see a shortcode.

  • Hi John, Thanks for the reply – I had only included the part relevant to the post_object field, full shortcode is below:

    function productlist_func( $atts ){
    
    $output = new WP_Query( array( 
    	'post_type' => 'product',
    	'tag' => $atts['tag'],
    	'posts_per_page' => 100, 
    	'no_found_rows' => true,
    	'update_post_term_cache' => false,
    	'update_post_meta_cache' => false,
    	'meta_key' => 'positioning', 
    	'orderby'=> array( 
           'meta_value' => 'DESC', 
           'date' => 'DESC' 
        )
    	)
    ); 
    	
    if ($output->have_posts()) :	
    
    $ret = '<div class="productcont">';
    
    while ($output->have_posts()) : $output->the_post();
    		
    $ret .= '
    
    <div class="productitem">
    	<div class="pilogo">
    		<div class="sqlogo">';
    		
    
    $brand = get_field('brand');
    if( $brand ) : 
    $ret .= '<img src="' . get_field('square_logo', $brand->ID) . '" />';
    endif;
    
    		
    $ret .=	'</div>
    	</div>
    	<div class="pidetails">
    		<div class="pititle">
    			<a href="' . get_permalink() . '">' . get_field('product_title') . '</a>
            </div>
    		<div class="pitext">' . get_field('description') . '</div>
    	</div>
    	<div class="eovisit">
    		<a href="' . get_permalink() . '" class="pbutton">Claim</a>
    		<div class="piterms">T&Cs apply 18+</div>
    	</div>
    </div>
    
    ';
    	
    endwhile; 
    
    wp_reset_postdata();
    	
    $ret .= '</div>';
    	
    return $ret;
    
    endif; 
    
    }
    add_shortcode( 'productlist', 'productlist_func' ); 
  • You are using this inside of a function (shortcode)

    The main thing that you’re missing is a reference $post that you need to add at the top in order to use the standard post loop

    
    global $post;
    

    However, I would not do this because when using a shortcode you can never know how deeply nested the custom queries are and wp_reset_postdata() will fail if your loop is nested inside an already existing secondary query loop.

    Instead of using have_posts() and I would not add the global reference above, I would do the following to avoid the potential problems

    
    if (count($output->posts)) {
      foreach ($output->posts as $post) {
        ....
        $brand = get_field('brand', $post->ID);
      }
    }
    

    So basically only use function that allow you to use the post ID rather than the normal posts loop.

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

The topic ‘Displaying post_object content within shortcode’ is closed to new replies.