ACF get_field not returning value - wordpress

I'm trying to use get_field to return a simple text field and it returns empty for some reason. A field itself is where it should be and there is text in it, so that part is all set. This PHP code is loaded via php snippet, post thumbnail for example, shows up perfectly. So everything works except for ACF field value.
<div class="your-class">
<?php
$args = array(
'post_type' => 'home_test',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query($args);
$brand = get_posts($args);
foreach ($brand as $post) {
setup_postdata($post);
$thumbnail = get_the_post_thumbnail_url($post->ID, 'full');
$homelinkvalue = get_field("home_brand_link");
if (!$thumbnail)
continue;
?>
<div>
<p><?php echo $homelinkvalue; ?></p><img src="<?php echo $thumbnail; ?>">
</div>
<?php
}
wp_reset_postdata();
?>
</div>

I think the issue is that you are mixing together a custom post loop (your foreach and setup_postdata()) but then are using functions like get_field(), which make use of the global post object. In this case, get_field() tries to lookup the field value by checking against the global $post, but it has not been properly set. See warning here about setup_postdata($post):
You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly.
You can implement this in your code with a slight modification:
global $post;
foreach ($brand as $currPost) {
$post = $currPost;
setup_postdata($post);
// Rest of code as normal
}
Or, since get_field() can accept a specific post as an argument instead of automatically using the global one, you could change:
$homelinkvalue = get_field("home_brand_link");
to:
$homelinkvalue = get_field("home_brand_link",$post->ID);
Side note: Normally, the recommended way to iterate posts is with the special "WP loop" pattern, something like:
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Do something -->
<?php endwhile; ?>
Using the above pattern automatically sets the global $post variable as it loops through, which allows devs to use functions like get_field() without having to worry about explicitly passing in a specific post; makes things a bit easier.

Try this one:
<div class="your-class">
<?php
$args = array(
'post_type' => 'home_test',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts) :
while($the_query->have_posts) : $the_query->the_post();
?>
<div>
<p><?php the_field( "home_brand_link" ); ?></p>
<img src="<?php the_post_thumbnail_url(); ?>">
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</div>

Related

Custom WP_Query won't print items to page, even though it contains the expected data

I have a WP_Query which contains data that I want to loop over on the page:
$query_posts_for_board_game = new WP_Query(get_posts(array(
'post_type' => $mm_custom_post_types,
'numberposts' => 20,
'meta_query' => array(
array(
'key' => array('board_game', 'board_games'),
'value' => get_the_ID(),
'compare' => 'LIKE'
)
)
)));
When I var_dump it I can see that it has the data in the query and query_vars properties but when I loop over it using the $query_posts_for_board_game->have_posts() method nothing is output. This code simply prints the else block.
<?php if($query_posts_for_board_game->have_posts()): ?>
<?php while ($query_posts_for_board_game->have_posts()) : $query_posts_for_board_game->the_post(); ?>
<?php get_template_part('template-parts/layouts/content', 'b1' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part('template-parts/layouts/content-none' ); ?>
<?php endif;?>
If I remove the call to WP_Query and just use get_posts I'm able to loop over it with a standard for loop, but then the nested templates can't take advantage of $post like they would with a normal loop:
<?php
// If there are posts
if ($posts_for_board_game) :
// Loop the posts
foreach ($posts_for_board_game as $board_game_post) :
?>
<?php echo $board_game_post->post_title . '<br />'; ?>
<?php
endforeach;
wp_reset_postdata();
?>
<?php endif; ?>
I seem to recall that this is because my custom query isn’t part of “the query”. Is there a way I can override “the query” so that my content can be output? Can I simply move my query further down the page after the other items are output?
I solved it! I'm sort of shocked that it was as easy as this.
I left the WP_Query in place, just like in the first query above. Then in my loop I simply changed the name of the iterator to $post and it started working. Here's the complete code for anyone else who has this question.
$query_posts_for_board_game = new WP_Query(get_posts(array(
'post_type' => $mm_custom_post_types,
'numberposts' => 20,
'meta_query' => array(
array(
'key' => array('board_game', 'board_games'),
'value' => get_the_ID(),
'compare' => 'LIKE'
)
)
)));
$posts_for_board_games = $query_posts_for_board_game->query;
and the output:
<?php if($posts_for_board_games): ?>
<?php foreach ($posts_for_board_games as $post): ?>
<?php get_template_part('template-parts/layouts/content', 'b1' ); ?>
<?php
endforeach;
wp_reset_postdata();
?>
<?php else: ?>
<?php get_template_part('template-parts/layouts/content-none' ); ?>
<?php endif;?>
I'm a little annoyed that I had to use an intermediate variable, to get at the query contents, but I'm fine with this code.

Custom Query breaking WP admin

I have this simple custom query that loops through my custom post type.
It is inside the plugins folder, activated, and is turned into a shortcode, which works perfectly.
The shortcode displays everything like it should. HOWEVER:
When i go to edit the page, I get a quick white page with the query results in it, then the editing admin page opens. And then my admin bar is also completely seperated from the left side.
And then when pressing on "edit", it gives me an error saying "Updating failed. Response is not a valid JSON-response".
This is driving me insane, I have done multiple of these before, but not for some reason my entire admin breaks just from a simple loop. What am i missing?
Thank you for your time!
function show_ambassadors($atts) {
$args = array(
'post_type' => 'ambassadorer',
'post_status' => 'publish',
'posts_per_page' => 8,
'orderby' => 'title',
'order' => 'ASC'
);
$loop = new WP_Query($args);
?>
<div class="ambassador-container">
<?php
if($loop->have_posts()) {
while ( $loop->have_posts() ) : $loop->the_post();
$thumbnail = get_post_thumbnail_id($post);
$image = wp_get_attachment_image_url($thumbnail, "thumbnail");
$categories = get_the_terms( $post->ID, 'category' );
?>
<div class="ambassador-inner-container">
<div class="ambassador-image" style="background-image: url('<?php echo $image ?>'); background-size: cover; background-repeat: no-repeat;"></div>
<h3 class="ambassador-title"><?php print the_title(); ?></h3>
<h4 class="ambassador-category"><?php foreach($categories as $cats) {echo ($cats->name);} ?></h4>
</div>
<?php
endwhile;
}
?>
</div>
<?php
}
add_shortcode('showamb', 'show_ambassadors');
When using the the_post() method of WP_Query, you manipulate global variables that are used by the “main WordPress loop”.
You need to use the wp_reset_postdata function in order to restore those global variables:
if($loop->have_posts()) {
while ( $loop->have_posts() ) : $loop->the_post();
[...]
endwhile;
wp_reset_postdata();
}

displaying standard WP get_content with 'Advanced Custom Fields'

I have the following loop that utilises Advanced custom fields (ACF) - Everything displays except the content. I have tried various solutions with no success.
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'sectors',
'orderby' => 'menu_order',
'order' => 'ASC'
));
global $post;
if($posts) {
?>
<?php
foreach($posts as $post) {
?>
<div class="content full sector">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
<?php
}
?>
<?php
}
wp_reset_postdata();
?>
Any help is greatly appreciated.
Thanks
When you use get_posts() to return post data, some post-related data is not available by default. One of these items is the_content(). This is resolved by calling an internal function setup_postdata(), with the $post array as its argument.
Solution: Add setup_postdata( $post ); within your foreach().
Read more in the Codex.

Displaying Custom Category loop in homepage

Thing is, In the homepage of my theme, I want to show post from different category in Different Div. Each DIV will contain 3 post from a category. I need a loop that can pick last 3 post from a specific Category. Can't find any suitable ans for it.
To explain things more easily, here is a demo picture of the Content section,
http://i.imgur.com/5QSzAIS.png
It will be a great help, if someone help me with the code !
<?php query_posts('cat=10&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This should get you started. You need to use this code twice. Where it says cat=10, you should enter your category ID (you can check this when you click on a Category from the admin panel, the the browser it will show something like this http://yourwebsite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=4&post_type=post)
Where it says tag_ID is the ID of your category.
I'm currently using a different method on a page of the site I'm building which allows me to run multiple loops in one page and specify the category for each one. This method I personally like better as it is more straightforward to me, and I can define the category with the slug instead of the ID.
Instead of using have_posts() and such, you use WP_Query() after defining your array and then wp_reset_postdata() to end your loop. The benefit is that you can keep running loops this way.
I'm also loading the data from custom fields in my posts using get_post_meta, but this method will work without that stuff.
<div class="audioGrid">
<?php
$args = array( 'post_type' => 'post',
'category_name' => 'audio',
'posts_per_page' => 3,
'order' => 'DESC' );
$query1 = new WP_Query($args);
while ( $query1->have_posts() ) {
$query1->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'audio-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'audio_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Audio Grid ?>
<?php wp_reset_postdata(); ?>
<div class="videoGrid">
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'order' => 'DESC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'video-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'video_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Video Grid ?>
<?php wp_reset_postdata(); ?>
Another cool thing I'm doing is using a custom field to define the order of things and using meta_key and meta_value_num to get that number and force the order how I want, and since this site isn't complicated, defining the order this way is convenient. I just use leading zeroes to make it easy: 001, 002, 003, etc
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'meta_key' => 'video_order',
'orderby' => 'meta_value_num',
'order' => 'ASC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
Anyway, hope this helps if you need to use multiple loops to pull posts from different categories.

Wordpress [Plugin: Event Post Type] How to arrange posts by event date instead of posted?

Currently using the Wordpress Plugin [Event Post Type] and the posts are currently ordered by when I post them. I really need them ordered by event date which the plugin adds to the post type for me.
I am currently using this code and am not sure how to get it to order:
<div id="events-teaser"><?php
$args = array( 'post_type' => 'event', 'posts_per_page' => 10, 'orderby' => '_date_start', 'order' => 'DESC' );
$loop = new WP_Query( $args );
if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="teaser-event <?php echo do_shortcode('[xydac_field]promo[/xydac_field]'); ?>">
<div class="event-meta gold">
<div class="event-date"><?php echo get_post_meta($post->ID, "_date_start", true); ?></div>
<div class="event-time"><?php echo get_post_meta($post->ID, "_time_start", true); ?></div></div>
<div class="event-title"><?php the_title(); ?></div></div><?php
endwhile; else:
?><p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
Place a call to query_posts() in one of your Template files before The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the $query_string global variable in the call to query_posts().
For example, to set the display order of the posts without affecting the rest of the query string, you could place the following before The Loop:
global $query_string;
query_posts( $query_string . '&order=ASC' );
change its order from 'ASC' to whatever you want.

Resources