Support

Account

Home Forums Front-end Issues Post Object: Selection does not show up in Custom Post Type

Solved

Post Object: Selection does not show up in Custom Post Type

  • Hi all!

    I have an ACF field called “banner_selection” which displays all “banner” post-types for selection. Basically if you don’t choose any banners, your sidebar shows 5 random banners. But if you made a (multiple) selection, the sidebar should show your selection be it 1 or 2 or 3 banners.

    So I made an ACF field called “banner_selection” and used this code in my theme file:

    <?php $post_objects = get_field('banner_selection');?>
    <?php if( $post_objects ):  // if banner selection was made, show those ?>
    	<?php foreach( $post_objects as $post): ?>
    		<?php setup_postdata($post); ?>
    		<?php get_template_part( 'banner','side' ); ?>
    	<?php endforeach; ?>
    	<?php wp_reset_postdata(); ?>
    
    <?php else: // if there are no selected banners, show random 5 banners ?>
    	<?php query_posts( array ( 'post_type' => 'banner', 'meta_key' => 'banner_type', 'meta_value' => 'sidebar', 'posts_per_page' => 5, 'orderby' => 'rand' ) ); // The Query ?>
    		<?php while ( have_posts() ) : the_post(); // The Loop ?>
    			<?php get_template_part( 'banner','side' ); ?>
    		<?php endwhile; ?>
    	<?php wp_reset_query(); // End Query, RESET ?>
    <?php endif; // end if banner selection was made ?>

    This feature works beautifully in my single.php but not single-company.php, that is, Company post-type singular pages somehow don’t seem to detect any selection made. Can I know what I’m doing wrong?

    Thanks in advance!

  • just to make sure..

    Do you have the field set up to both regular posts as well as company posts?

    Also check if there’s a difference in the two files for if the code is inside or outside the loop.

  • Hi Jonathan,

    Yes the code is in banners.php which I’ve included in both single.php and single-company.php, thus I don’t see how come it doesn’t work on single-company.php but only on single.php 🙁

    If it please you here is the code in its entirety on banner.php:

    <?php if ( is_single() || is_singular() ) : // for singular items only ?>
    
    	<?php while ( have_posts() ) : the_post(); ?>
    
    		<?php $post_objects = get_field('banner_selection');?>
    		<?php if( $post_objects ):  // if banner selection was made, show those ?>
    			<?php foreach( $post_objects as $post): ?>
    				<?php setup_postdata($post); ?>
    				<?php get_template_part( 'banner','side' ); ?>
    			<?php endforeach; ?>
    			<?php wp_reset_postdata(); ?>
    
    		<?php else: // if there are no selected banners, show random 5 banners ?>
    			<?php query_posts( array ( 'post_type' => 'banner', 'meta_key' => 'banner_type', 'meta_value' => 'sidebar', 'posts_per_page' => 5, 'orderby' => 'rand' ) ); // The Query ?>
    				<?php while ( have_posts() ) : the_post(); // The Loop ?>
    					<?php get_template_part( 'banner','side' ); ?>
    				<?php endwhile; ?>
    			<?php wp_reset_query(); // End Query, RESET ?>
    		<?php endif; // end if banner selection was made ?>
    
    	<?php endwhile; ?>
    
    <?php else : // for homepage/archive items only ?>
    
    	<?php query_posts( array ( 'post_type' => 'banner', 'meta_key' => 'banner_type', 'meta_value' => 'sidebar', 'posts_per_page' => 5, 'orderby' => 'rand' ) ); // The Query ?>
    		<?php while ( have_posts() ) : the_post(); // The Loop ?>
    				<?php get_template_part( 'banner','side' ); ?>
    		<?php endwhile; ?>
    	<?php wp_reset_query(); // End Query, RESET ?>
    
    <?php endif; // is_single() ?>

    On your second point though, the code is outside the loop. This *could* be the case, however I still have no idea why it wouldnt work for single-company.php when it works for single.php.

    Many thanks in advance!

  • Hi @nataliette

    Sounds like you need to do some debugging and make sure that ACF can load the value as expencted.

    Start by testing the value of:

    
    global $post;
    echo $post->ID;
    

    Is the global $post->ID the correct Company ID?

    Your code uses the setup_postdata function. This can be seriously dangerous if you don’t fully understand what it is doing to the global objects.

    Some simple debugging through your template files will help to find the issue.

    Good luck!

  • Hi @elliot,

    I tried inserting
    <?php global $post; echo $post->ID; ?>
    and it gives me 862. But the post-id of the company-post-type is 240 (am I correct to say the post ID is the number in this URL: ../adm/wp-admin/post.php?post=240&action=edit )

    If I am correct in the above 2 things, this means that ACF isnt loading the selected banners from the company’s post-id and instead some other post’s. I am a newbie at php, would be really grateful if you could let me know how I can modify my code in my banners.php (which is called in my single-company.php) which has the code that I pasted in my previous post.

    EDIT: I have tried including the banners.php template INSIDE my loop and it works! Is there a way to make it work outside the loop, if I do not wish to create a secondary loop?

    P/S. Thanks for helping me hone in on the root of the problem! xx

  • Hi @nataliette

    I think this can be easily solved by loading the value directly from the specific post like so:

    
    get_field('banner_selection', 240);
    

    You can read more about this second parameter in the docs.

    Thanks
    E

  • Hey @elliot,

    Thanks for the suggestion! But as I have various companies, can the second parameter be generated dynamically?

    Or would simply creating a second loop suffice, much more easily?

    I’d value your input.

    Thanks!

  • Hi Nataliette,

    If your get_field function runs after the first loop you could simply add the post id to a variable inside the loop and use that. Like:

    
    $post_id = get_the_ID();
    

    Then use it in the get_field:

    
    
    get_field('banner_selection', $post_id);
    
  • @Jonathan: YOU. ARE. AWESOME.

    Here’s the code I used outside the loop to generate the field output for anyone else who’s searching to solve the same problem.

    
    <?php 
    $post_id = get_the_ID(); // if ACF is outside loop, use this to get post ID
    $post_objects = get_field('banner_selection', $post_id); // use second parameter to get the fields from this post
    echo $post_id // Optional: use this to check if the ID is correct for this post
    ?>
    <?php if( $post_objects ):  // if banner selection was made, show those ?>
    
    <?php foreach( $post_objects as $post): ?>
    	<?php setup_postdata($post); ?>
    	<?php get_template_part( 'banner','side' ); ?>
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    
    <?php endif; // end if banner selection was made ?>
    
    

    To output selection fields for Post Object fieldtype in ACF.

  • No worries @nataliette 🙂

    Glad to help, keep up the good work!

    Here’s actual proof that I’m awesome (if you have a spotify account):
    http://open.spotify.com/track/3RIkLl4v8Ex27OpIwTZSIu

  • This reply has been marked as private.
Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Post Object: Selection does not show up in Custom Post Type’ is closed to new replies.