Support

Account

Home Forums ACF PRO Retrieve data from post-object them the post has been created with ACF comp.

Solved

Retrieve data from post-object them the post has been created with ACF comp.

  • Hi, I want to give my client the possibility to choose not only what posts (in this case, news) he will decide to display on the home page, but the order that they will be displayed, absolutely based on his criteria (not filtered by data, randomly etc).

    So, I have created on home page flexible content in ACF within a post-object inside, to give the client the possibility to 1. select the post, 2. order the posts.

    I have got displayed the title of the selected posts with this code:

    <?php
    
    		if( have_rows('news_home_list') ): while ( have_rows('news_home_list') ) : the_row();
    
    			if( get_row_layout() == 'news_home_list_plus' ):
    				$news = get_sub_field('news_home_select');
    					
    				if( $news ): ?>
    					<strong><?php echo esc_html ( $news->post_title ); ?></strong>
    				<?php endif; ?>
    				
    			<?php endif; ?>
    		<?php endwhile; ?>
    		<?php endif; ?>

    But I need to show the categories, the title and the abstract of each selected post, all fields created with ACF components, and yes, they will be linked to the entire post. Can I do this?

  • You just need to supply the post ID for WP and ACF. Going by your code

    
    // use a wp function 
    $excerpt = get_the_excerpt($news->ID);
    // use an acf field
    $value = get_field('field_name', $news->ID);
    

    I am assuming from your code that you are using a post object field. You can also use setup_postdata() as explained in the documentation.

  • Hi, thanks for the response but or I don’t understand it, or my case is a lit bit diverse that I’d describe for you.

    I try to use

    $value = get_field('field_name', $news->ID);

    directly in the flexible content template tag but it doesn’t work. See it:

    <div class="news-group">
    
        <?php
    
            if( have_rows('news_home_list') ): while ( have_rows('news_home_list') ) : the_row();
    
                if( get_row_layout() == 'news_home_list_plus' ):
                    $news = get_sub_field('news_home_select');
                    $title = get_the_title($news->ID);
                    $abstract = get_field('news_abstract', $news->ID);
                    ?>
    
                    <div class="news">						
                        <strong><?php $title; ?></strong>
                        <p><?php $abstract; ?></p>						
                    </div>
                        
    
                <?php endif; ?>
            <?php endwhile; ?>
            <?php endif; ?>		
    
    </div>

    I try to use setup_postdata() doc example but nothing, 🙁

    <div class=”news-group”>
    
        <?php
    
            if( have_rows(‘news_home_list’) ): while ( have_rows(‘news_home_list’) ) : the_row();
    
                if( get_row_layout() == ‘news_home_list_plus’ ):
                    $news = get_sub_field(‘news_home_select’);
                    $featured_posts = get_field(‘news_home_select’);
                        if( $featured_posts ): ?>
    
                        <?php foreach( $featured_posts as $post ):
    
                            // Setup this post for WP functions (variable must be named $post).
                            setup_postdata($post); ?>
                                <div class=”news”>
                                    <div class=”news-content”>
                                    <a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
                                    <span>A custom field from this post: <?php the_field( ‘news_abstract’ ); ?></span>
                                </div>
                        <?php endforeach; ?>
    
                        <?php
                        // Reset the global post object so that the rest of the page works correctly.
                        wp_reset_postdata(); ?>
                        </div>
                        <?php endif; ?>
    
                <?php endif; ?>
            <?php endwhile; ?>
        <?php endif; ?>
    
    </div>

    I remember that my post object is into flexible content…

  • I try to resolve in another way, and the result now is “array”. Where is my mistake?

    <div class="news-group">
    
    <?php
    
        if( have_rows('news_home_list') ): while ( have_rows('news_home_list') ) : the_row();
    
            if( get_row_layout() == 'news_home_list_plus' ):
                $news = get_sub_field('news_home_select');
                $title = get_the_title($news->ID);
                $values = get_field('news_abstract', $news->ID);
                ?>
    
                <div class="news">						
                    <strong><?php echo $title; ?></strong>
    
                    <?php	
                    if( $news ): ?>
                        <p><?php echo ( $news->$abstract ); ?></p>
                    <?php endif; ?>
    
                </div>							
    
            <?php endif; ?>
        <?php endwhile; ?>
        <?php endif; ?>		
    
    </div>
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.