Support

Account

Home Forums Add-ons Flexible Content Field Pass field from Flexible Content into a widget

Solving

Pass field from Flexible Content into a widget

  • Hi Elliot,

    I’m using the Relationship Field inside the Flexible Content Field add-on. The box is used to create Featured Posts. The thing is that i’m also trying to show the same posts, added with relationship field, inside a sidebar widget. As you understand this have no affect, the posts are just do not show. I guess more coding is required to make it possible or is it possible at all?

  • Hi @vasili

    Can you please elaborate more on the question? I don’t understand what you have and what you are trying to do.

    Perhaps some screenshots to show the data structure, and a better explanation of what you mean by:
    The thing is that iā€™m also trying to show the same posts, added with relationship field, inside a sidebar widget. As you understand this have no affect,

    Thanks
    E

  • Hi Elliot, thank you for getting back to me.

    I’m using the Relationship field with Flexible Content to create a list of selected posts which is called Featured Posts:

    <?php
    while( has_sub_field( 'page_builder' ) ):
       if( get_row_layout() == 'featured_posts' ):
          get_template_part ( 'builder/featured', 'posts' );
       endif;
    endwhile;
    ?>

    The loop inside featured-posts.php looks like this:

    <?php 
    $featured_posts = get_sub_field( 'featured_post_add' );
       if( $featured_posts ):
          foreach( $featured_posts as $post ): ?>
    
             <article>
                ...
             </article>
    
    <?php
          endforeach;
          wp_reset_postdata();
    endif;
    ?>

    Everything works great.

    Now what i’m trying to do, is to output the exact same posts, added in this Relationship field, in a widget. I have created a widget file featured-posts-widget.php which i drag into the sideabr. The thing is that the posts are not showing up and the widget is blank, besides the title, no matter what type of loop or a query i use. The code among others i tried is:

    
    <?php
    global $post;
    $featured_posts_sidebar = get_field( 'featured_post_add' );
       if( $featured_posts_sidebar ):
          foreach( $featured_posts_sidebar as $post ): ?>
    
             <article>
                ...
             </article>
    
    <?php
          endforeach;
          wp_reset_postdata();
    endif;
    ?>

    In the widget i use get_field and not get_sub_field. Maybe using WP_Query and not the foreach loop would be a solution, but what parameters do i have to use?

    Can you point me to the right direction?

  • Hi @vasili
    Thanks for the info.

    It is most likely that within a widget, ACF does not know which post to load the value from.

    You can test this by adding the following code above global $post;

    
    $post_id = get_field_ID();
    echo '<pre>';
    	var_dump( $post_id );
    echo '</pre>';
    

    Can you check the variable value and compare against the expected post ID?

    Thanks
    E

  • Hi Elliot,

    I get bool(false) on this field

  • Hi @vasili

    If get_field_ID() (not a field, but a WP variable) is false, then WP does not have a global $post at the time of your widget.

    This explains why ACF is not able to load.

    Where in the template is this widget loaded?
    On what page is the widget loaded, or perhaps all?

    You will need to find a way to get a post_id (numeric value) for ACF to load the value from.

    Thanks
    E

  • Hi Elliot

    Widget loads in all pages with sidebar; homepage, single, categories.
    When i use get_field_ID(); above global $post; i get an undefined function error, in a case of ACF field i get the “false” result.

    Another test i made is commenting the if statement: if( $featured_posts_sidebar ): i get the loop error: Invalid argument supplied for foreach()

  • Hi @vasili

    ACF needs a $post_id so that it knows which post to load from.

    If get_field_ID() returns an error, then ACF will not be able to load data.

    Does it make a difference if you place the function BELOW global $post;?
    Does global $post; echo $post->ID; produce the correct post_id?

    Thanks
    E

  • Hi Elliot

    It does not make a difference, the function still returns an error.
    The echo $post->ID; returns an ID of the current single post i’m viewing.

    I had and idea of making a field, true/false maybe, and assign it to post edit page with location rule. As soon as the post is selected in the Relationship field and page is updated, this true/false is getting the “true” value. Then in the widget, i can use this field in WP_Query as meta_key parameter. The schema looks like this:
    Relationship fields in Flexible Content (Page) → True/False field (Post) → Widget.

    Do you think something like this is possible using Filters?

    I just don’t have another ideas how to sort out the situation.

    Thanks again for your help.

  • Hi @vasili

    Can you please remove any variables from the equation completely?

    You can do this by harcoding the $post_id parameter of the get_field function like so:

    
    $value = get_field('field_name', 123);
    

    Where 123 is the post ID where you want to load the data from.

    If ACF can’t load data like this, then the chosen post does NOT contain the data you expect and you will need to double check all your field names / post ID’s are correct.

    Thanks
    E

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

The topic ‘Pass field from Flexible Content into a widget’ is closed to new replies.