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?
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,
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?
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
$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?
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.
Widget loads in all pages with sidebar; homepage, single, categories.
When i use
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()
ACF needs a $post_id so that it knows which post to load from.
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; echo $post->ID; produce the correct post_id?
It does not make a difference, the function still returns an error.
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.
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.
The topic ‘Pass field from Flexible Content into a widget’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2022 Advanced Custom Fields.