Support

Account

Home Forums General Issues Pulling Custom Post Data into a Loop – using it throughout the loop

Solved

Pulling Custom Post Data into a Loop – using it throughout the loop

  • I may have created something that might be impossible, if not really confusing.

    I have a custom post type of “containers.” That CPT has data such as name and price.

    I then have a category.php that uses WP_query to query the posts just for the category that it is currently on.

    That category.php needs to pull data from the CPT – basically, for every product in a category, it needs to display the correct price from the custom post type it goes with.

    Example: “Lavender” category pulls six containers, each container has its own price.

    Currently, the below is pulling the right posts that fit the category it is currently on:

    <?php
    				$cat = get_queried_object();
    				$cat_id = $cat->cat_ID;
    				$custom_query = new WP_Query( 
    					array( 
    						'post_type' => array('post', 'products'),
    						'cat' => $cat_id
    					)
    				);
    				if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

    This part is working great. But I want the “price” below to pull the price from the category CPT based on the post it pulled – X container product is XX price, Y container product is YY price.

    <a href="<?php the_permalink(); ?>" title="<?php the_title();?>: $<?php the_field('base_price'); ?>+">
    							<p><?php the_title();?></p>
    						
    							<p class="price">$<?php the_field('base_price'); ?>+</p>
    						</a>

    So that the end result would be a page saying:
    -Lavender Jar Candle $20
    -Lavender Tin Candle $15
    -Lavender Bath Wash $10

    Where “lavender” is the current category, “jar candle” “tin candle” “bath wash” is the entries into the custom post type “container,” “$20” “$15” “$10” is the price pulled from the custom post type container associated with that particular container.

    Ideally, I would like to say “hey, pull the title from the category post, pull the price from the CPT that it is associated with.”

    Is there a way to do this, or am I making things way more complicated than they should be?

    P.S. This gets me part of the way there (inside my loop from code above), but as you see, it leaves out the “price” above the $post_object. I would like for it to pull the price of the CPT, but leave the_title as the title from the category post it pulled:

    <li>
    						<?php // check if the post has a Post Thumbnail assigned to it.
    							if ( has_post_thumbnail() ) {
    								echo "<a href=";
    								the_permalink();
    								echo ">";
    								the_post_thumbnail('thumbnail');
    								echo "</a>";
    							}
    						;?>
    						<a href="<?php the_permalink(); ?>" title="<?php the_title();?>: $<?php the_field('base_price'); ?>+">
    							<p><?php the_title();?></p>
    							<?php
    								$post_object = get_field('container');
    								if( $post_object ): 
    									// override $post
    									$post = $post_object;
    									setup_postdata( $post ); 
    							?>
    							<p class="price">$<?php the_field('base_price'); ?>+</p>
    							<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    							<?php endif; ?>
    						</a>
    					</li>
  • If I understand you right, you already have what you need. You already have

    
    $cat = get_queried_object();
    $cat_id = $cat->cat_ID;
    

    anywhere that you want to output the category name just do

    
    echo $cat->name;
    
  • Hi John,

    Thank you for your answer! I’m actually wanting to output the “base_price” that is from a CPT.

    This works:

    <?php
    								$post_object = get_field('container');
    								if( $post_object ): 
    									// override $post
    									$post = $post_object;
    									setup_postdata( $post ); 
    							?>
    							<p class="price">$<?php the_field('base_price'); ?>+</p>
    							<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    							<?php endif; ?>'

    But I can’t get it to work when I move it above the first place where I have base_price without it calling the wrong title.

    I want it to pull:
    Current post link
    Current post title
    Price of the CPT of “containers” attached to post.
    Current post title
    Price of the CPT of “containers” attached to post.

    In that order. Which means I have to bounce between the current loop pulling the category info, to then the CPT of containers, to back to the category info, then back to the CPT again.

    I hope that makes sense.

  • it’s hard for me to figure out what you’re doing with the way the code is all chucked up. Can you post all of your code in one block.

    Also, why are you doing this? category.php already pulls the posts from the current category, so this seems to be an extra query doing the same thing that is already done for you. If this is to just filter out posts to just get posts of the post type then this should be done with a pre_get_posts filter. The reason I ask is that your using wp_reset_postdata() and because you have a nested custom query this is probably not working the way you think it’s working.

    
    $cat = get_queried_object();
    				$cat_id = $cat->cat_ID;
    				$custom_query = new WP_Query( 
    					array( 
    						'post_type' => array('post', 'products'),
    						'cat' => $cat_id
    					)
    				);
    				if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    
    
  • Hi John,

    The query is just to filter out the “product” custom post type, as the category could also be the scent too.

    `<?php get_header(); ?>

    <!– category.php–>

    <div id=”pagecontent” class=”contentwidth”>

    <h1><?php single_cat_title(); ?></h1>

    <section class=”productslist”>

    <ul class=”list-items categories”>
    <?php
    $cat = get_queried_object();
    $cat_id = $cat->cat_ID;
    $custom_query = new WP_Query(
    array(
    ‘post_type’ => array(‘post’, ‘products’),
    ‘cat’ => $cat_id
    )
    );

    if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

    <li>
    <?php // check if the post has a Post Thumbnail assigned to it.
    if ( has_post_thumbnail() ) {
    echo “<a href=”;
    the_permalink();
    echo “>”;
    the_post_thumbnail(‘thumbnail’);
    echo “</a>”;
    }
    ;?>
    <a href=”<?php the_permalink(); ?>” title=”<?php the_title();?>: $<?php the_field(‘base_price’); ?>+”>
    <p><?php the_title();?></p>
    <?php
    $post = get_field(‘container’);
    setup_postdata( $post );
    ?>
    <p class=”price”>$<?php the_field(‘base_price’); ?>+</p>
    <?php wp_reset_postdata(); // IMPORTANT – reset the $post object so the rest of the page works correctly ?>
    </a>
    </li>
    <?php endwhile; wp_reset_query();?>
    </ul>

    <?php else : ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>

    </section><!–end .categorylist–>

    </div><!–end #pagecontent.contentwidth–>
    <?php get_footer(); ?>
    `
    The whole point was to make it easier to update repeating pieces of content.

    Each product has a scent and container that is a repeating piece of content.

    There could be 50 products with one scent – having the scent as a CPT allows me to edit the scent info without editing 50 products at once. Same with container.

    Products are priced by container. So over the long term. it’s easier to change the container price over a few containers than it would be to change 200+ products pricing.

    The setup_postdata I got from somewhere in these forms and stuck with it because it worked on part of the code.

  • Circling back to this to provide a solution for someone who may be looking for it.

    I had three custom post types before:
    product
    container
    scent

    I changed the container and scent from custom post type to taxonomy.

    Again, the goal was to put content in a format that I could pull, based on the product custom field values (using ACF custom fields).

    Thus: Lavender Mason Candle $20

    Where “Lavender Mason Candle” is the product, “lavender” is the scent, and “mason candle” is the container. The $20 comes from a custom field on the container taxonomy.

    Solution (this is for a taxonomy.php page, but the main part at the bottom could very well work on others.)

       <ul>
    	<?php
    		// Get the current page taxonomy term info
    		$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 	
    										
    		// args
    		$args = array(
    			'post_type'   => 'products', //
    			'tax_query'   => array(
    				array(
    					'taxonomy' => 'scents',
    					'field'    => 'slug',
    					'terms'    => $term,
    				),
    			)
    		);
    
    		// New Query
    		$custom_query = new WP_Query( $args );?>
    					
    		<?php if( $custom_query->have_posts() ) : while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    				  	
    			<li>
    				<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
    				<!-- For part of the title, I want to temporarily pull info from the $container -->
    				<a href="<?php the_permalink(); ?>" title="<?php the_title();?>: $<?php $container = get_field('container'); if( $container ): the_field('base_price', $container); ?>+<?php endif; ?>">
    				<!-- After the title, I want to revert back to the original post data-->					
    					<p><?php the_title();?></p>
    					<!--Now back again to the container-->	
    					<?php $container = get_field('container'); if( $container ): ?>
    					<p class="price">$<?php the_field('base_price', $container); ?>+</p>
    					<?php endif; ?>
    					<!--And back again to the original post data-->
    				</a>
    								
    			</li>
    		<?php endwhile; wp_reset_postdata();?>
    </ul>

    This is the main worker to help selectively pick what goes from where:

        <?php $container = get_field('container'); if( $container ): ?>
    					<p class="price">$<?php the_field('base_price', $container); ?>+</p>
    					<?php endif; ?>

    Hope this helps someone else and saves them three days of searching for answers for something similar.

  • Sorry I did not get back to this sooner, glad that you worked out a solution that works for you.

  • Hi Amber

    Can I just say thank you very much for updating your query with the detailed solution.

    I have been battling with a very similar problem all afternoon which is now solved thanks to your post.

    Have a good weekend

    Kind Regards
    Brad

  • Hi Brad, Glad I could help you out! It took me a week and two other people to help me figure it out! It’s great to hear you were able to figure it out too!

  • Hi Amber

    You certainly saved me some time …

    The only tweak I needed to make to your code was for one section where I allowed multiple Post Objects to be selected – using your example I added an s to – $containers and using foreach $containers as $container – to allowed me to display multiple records (prob no use for base price but if you were showing say – container colors or something you could select multiple and they would all show)

    
    <?php $containers = get_field('container'); foreach( $containers as $container): if( $container ): ?>
    <p class="price">$<?php the_field('base_price', $container); ?>+</p>
    					<?php endif; ?>
                                            <?php endforeach; ?>
    

    Thanks Again

    All the best
    Brad

  • That’s great! (It’s been a few months since I last worked on this so I’m a bit fuzzy on the details).

    Yeah I didn’t have an exact need to show multiple records. I just wanted to be able to pull content from one area and then content from another. That’s what mainly started this whole thing – the price was based on the container, not the scent.

    Essentially I needed to bounce back and forth between two different taxonomies.

    It was tricky, but it works well (here’s the result of the exact code above) https://candleflare.com/scents/monkey-farts/

    The page is to pull all of the posts that have the scent taxonomy term “monkey farts” then display them, but while doing that, pull the base price from the container taxonomy associated with it (price is based on the container, not the product).

    What started this was being able to change the pricing at the container level, not product level, since this particular use case the prices are dependent on container, then added amount for the wick.

    Glad that I helped save you time! This bugged me for a week so I’m glad you were able to get it done in less time!

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

The topic ‘Pulling Custom Post Data into a Loop – using it throughout the loop’ is closed to new replies.