Support

Account

Home Forums Front-end Issues custom field group not displaying in wp query loop for a custom post

Solving

custom field group not displaying in wp query loop for a custom post

  • I cannot figure out why this loop won’t show the ACF custom field testimonial_content on the front-end. Any ideas? Thanks.

    
    	echo	'<div class="grid grid-3">';
    
    				$args = array(
    					'posts_per_page' => -1,
    					'post_type' 			=> "testimonial",
    					'post_status' 		=> "publish",
    				);
    
    						//loop
    						$testimonials = new WP_Query($args);
    
    						while($testimonials-> have_posts()): $testimonials->the_post(); ?>
    
    							<div>
    									<?php if( get_field('testimonial_content') ): ?>
        									<p class="swd_testimonial_content"><?php the_field('testimonial_content'); ?>
    
    									<?php endif; ?>
    
    							</div>
    
    						<?php endwhile;
    						wp_reset_postdata();?>
    			</div>
    
  • Could it be that there is white space in this line, between “$testimonials-> have_posts()”?
    while($testimonials-> have_posts()):

  • There is nothing in what has been provided that indicates a problem.

    Is the query returning any posts?

    Can you supply more information about the field group, field?

  • Also this line should end with a p tag:
    <p class="swd_testimonial_content"><?php the_field('testimonial_content'); ?>

  • Pls see the attachment of the Testimonial Entry fields.

    I have created a post-type Testimonial. The admin opens a “new” testimonial and enters the info in the ACF fields. This should save it in database.

    Then, I created a bock called Testmonial Grid where the admin can drop it in a page and select the number of testimonials to show and the number per row in the grid. The block redirects to the template testimonial.php in the blocks folder. The template should grab the data from the block and the data that was already entered in the database to render the testimonials on the template.

    The code I shared before won’t show any of the data the was entered in ACF fields. But it will show data that is part of a typical WordPress post like the Title. So, I am gathering the get_field() isn’t grabbing the meta-data that is should and I can’t figure out why.

    I have attached a screenshot of the fields. Thanks for your help.

  • Here is the full code on the page. I am not using namespace b/c that causes problems when I do, so I commented out the namespace.

    <?php
    /**
    * Testimonial block
    *
    * @package SWD\swd_realtor_theme
    * @subpackage Testimonial Block
    * @author Superfly Web Designs
    * @since 1.0.0
    * @license GPL-2.0+
    **/
    //namespace SWD\swd_realtor_theme;

    echo ‘<section id=”testimonial_grid” class=””>’;

    $testimonial_include_image = get_field(‘testimonial_include_image’);
    $testimonial_how_many = get_field(‘testimonial_how_many’);
    $testimonial_cols = get_field(‘testimonial_cols’);
    $testimonial_author = get_field(‘testimonial_author’);
    $testimonial_content = get_field(‘testimonial_content’);
    $testimonial_image = get_field(‘testimonial_image’);

    echo ‘<div class=”grid grid-3″>’;

    $args = array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => “testimonial”,
    ‘post_status’ => “publish”,
    );

    //loop
    $testimonials = new WP_Query($args);

    while($testimonials->have_posts()): $testimonials->the_post(); ?>

    <div>
    <?php if( get_field(‘testimonial_content’) ): ?>
    <p class=”swd_testimonial_content”><?php the_field(‘testimonial_content’); ?></p>
    <?php endif; ?>

    </div>

    <?php endwhile;
    wp_reset_postdata();?>
    </div>
    </section>

  • and here is the block type info

  • $testimonial_include_image = get_field(‘testimonial_include_image’);
    $testimonial_how_many = get_field(‘testimonial_how_many’);
    $testimonial_cols = get_field(‘testimonial_cols’);
    $testimonial_author = get_field(‘testimonial_author’);
    $testimonial_content = get_field(‘testimonial_content’);
    $testimonial_image = get_field(‘testimonial_image’);

    The above code is outside the loop. the_field and get_field functions should have a post ID if not such ID is given.
    https://www.advancedcustomfields.com/resources/get_field/

  • Okay, when I make a page template, it works. So for some reason, it won’t pull the fields from the database via the block if they aren’t entered into the block. How can I have a block render custom fields that were previously entered in the database through ACF?

    Big picture, is I want the client to place the testimonials grid block on any page and set the number of testimonials and number of rows. Then I want the info to be pulled from the database to show it according to the block settings.

    Is this possible?

    Thanks.

  • Grid block?
    Have you followed this setup guide?
    https://www.advancedcustomfields.com/resources/blocks/

  • Yes- the block works but it doesn’t pull up the ACF fields from the database that were entered outside of the block. The block only pulls what was entered via the block fields and general WP fields like title and content. I want the ACF custom fields to be pulled. Thoughts?

  • A similar situation would be using a block to pull up a grid of latest posts. But instead of that, I want it to pull testimonials (custom post type with custom fields).

  • can you print out to the screen how many testimonials this var contains: var_dump($testimonials)

  • I need a way to pull the custom fields data saved in meta values as well as the block data saved in html comments- and then render that data in a grid loop.

  • Which part is not working? Can you be more specific?

  • Actually, it looks like the ACF block stores data in the the post content field as data. This is an example from a testimonial block entry.

    <!– wp:acf/testimonial-grid {
    “id”: “block_607ed8e56b83a”,
    “name”: “acf\/testimonial-grid”,
    “data”: {
    “testimonial_how_many”: “15”,
    “_testimonial_how_many”: “field_607d929e07f46”,
    “testimonial_how_many_cols”: “3”,
    “_testimonial_how_many_cols”: “field_607de7e495897”,
    “testimonial_include_image”: “0”,
    “_testimonial_include_image”: “field_607df0ae5e562”,
    “testimonial-background_color”: “#f7f7f7”,
    “_testimonial-background_color”: “field_60564f9d5d72a”,
    “testimonial-text_color”: “”,
    “_testimonial-text_color”: “field_60564fc05d72b”
    },
    “align”: “”,
    “mode”: “auto”
    } /–>

    So I want to grab some of this info and testimonial custom fields data in post meta values and render it on the front end of a block.

  • Vayu, Thank you for your help on this. I can print the testimonials, but it only pulls the WP fields, not the custom fields. How can I access the custom fields that were entered on a custom post type. I see that data in the meta values and get_field is supposed to get meta_values, so not sure why it isn’t working.

  • Okay, so the $testimonials variable has found x number posts?
    Are you using the correct post ID inside the_field() and get_field()?

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.