How to display acf image from image array - php

I have this query -
<?php if (has_term( 'channel', 'listing_area' )) { ?>
<?php
$posts = get_posts(array(
'numberposts' => 3,
'post_type' => 'adverts',
'order_by' => 'title',
'order' => 'random',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'associate_adverts',
'value' => '1822',
'compare' => 'LIKE',
)
),
));
?>
<?php //if it's returning the object
foreach($posts as $advert){
$img = get_field("advert", $advert->id);?>
<img src="<?php echo $img["url"]; ?>"/>
<?php }?>
I need to show an image called from an advanced custom field called 'advert' which is set to use the image array option where I'm calling the post title.
How can I do this? Thanks.

Read the get_field doc
In your loop, use $img = get_field("advert", $advert->ID), return an array.
Then to get the url, use $img["url"]
The doc

Related

How to query to get the current custom post’s value in a single post page?

I put the code below in a single post page.
The problem is it displays correct title of the current post but incorrent count number.
It shows count 0, when the actual count is 1.
Current post has purchased=yes and firstsubmission=yest, so it should display 1.
I trid 'post_parent' => get_the_ID(), instead of 'title' => get_the_title(), and 'posts_per_page' => 1, but still shows count 0.
Would you please help me correct the code?
$info_args = [
'author' => get_current_user_id(),
'title' => get_the_title(),
'posts_per_page' => -1,
'post_type' => 'infosubmission',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'purchased',
'value' => 'yes',
'compare' => '=',
'key' => 'firstsubmission',
'value' => 'yes',
'compare' => '=',
)
)
];
$info_info_posts = get_posts($info_args);
$info_info_count = count($info_info_posts);
echo 'Title: ' . get_the_title() .'<br>';
echo 'Count:' .$info_info_count ;
Thank you.
If you are simple trying to get a count of all the posts belonging to a certain custom post type, just use this instead, as it's much faster:
$info_info_count = wp_count_posts( 'infosubmission' )->publish;
Unfortunately this function won't let you filter by anything except post status, so if that's required, then you'll need to create a secondary query.
Firstly, you can't query this way by title, so leave that out (it would have restricted your query to only the current post/page anyway). Second, your meta_query argument isn't formatted correctly for multiple fields. Try this instead:
$info_args = [
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_type' => 'infosubmission',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'purchased',
'value' => 'yes',
// 'compare' is '=' by default, so not needed here.
),
array(
'key' => 'firstsubmission',
'value' => 'yes',
),
),
];
$info_info_posts = get_posts( $info_args );
$info_info_count = count( $info_info_posts );
echo 'Title: ' . get_the_title() . '<br>';
echo 'Count: ' . $info_info_count;
Additionally, functions like get_the_title() use the global $post object by default, so unless you provide an argument or call the_post() again, it will only get the title of the current page. So I'm not sure exactly what you're trying to do here, but if you wanted to, for example, print the title of each post returned by your new query, you would need to start another loop to iterate through the results. In that case I would recommend creating a new WP_Query independent of the main one:
$info_posts = new WP_Query( $info_args );
$count_info_posts = $info_posts_query->found_posts;
echo 'Count: ' . $count_info_posts . '<br>';
//Start The Loop
while ( $info_posts->have_posts() ) : $info_posts->the_post();
echo 'Title: ' . get_the_title() .'<br>';
// Do anything else you'd like with each post's data here.
endwhile;
// Restore original Post Data
wp_reset_postdata();
Note on multiple queries from the WordPress docs:
Note: If you use the_post() with your query, you need to run wp_reset_postdata() afterwards to have template tags use the main query’s current post again.
Your meta query wrong you have to make a separate array for each meta key compare. check the below code.
$info_args = [
'author' => get_current_user_id(),
'title' => get_the_title(),
'posts_per_page' => -1,
'post_type' => 'infosubmission',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'purchased',
'value' => 'yes',
'compare' => '=',
),
array(
'key' => 'firstsubmission',
'value' => 'yes',
'compare' => '=',
)
)
];
$info_info_posts = get_posts($info_args);
$info_info_count = count($info_info_posts);
echo 'Title: ' . get_the_title() .'<br>';
echo 'Count:' .$info_info_count ;

WP_Query exclude posts using meta query

Can't wrap my head around this one, would appreciate some help!
Overview
I have a "news" page with two seperate WP_Query loops. The first loop at the top of the page contains the 2 latest "featured" posts, defined by the following conditions;
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'posts_per_page' => 2,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => 'yes',
'compare' => 'LIKE',
),
),
);
The "featured_post" key relates to a custom ACF checkbox field. The client uses this to define featured posts in the backend of Wordpress.
Now in the second WP_Query loop, the remaining posts are displayed using the following conditions;
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
),
);
This essentially excludes the featured posts from this second loop.
Problem
My issue is the client doesn't always remember to untick the "featured" ACF field from the old featured posts when adding a new one. Therefore older featured posts that are no longer meant to be featured don't shift into the second loop.
Is there a way to use the meta_query in the second loop to exclude only the two latest featured posts - instead of all of them?
Or is there a better way?
I hope this will help you.
First, create a function on function.php like this:
<?php
function exclude_featured_post_IDs(){
$featured_post_IDs = array();
$args_featured = array(
'post_type' => 'post',
'orderby' => 'date',
'order' =>'DESC',
'posts_per_page' => 2,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '"yes"',
'compare' => 'LIKE'
),
)
);
$query_featured = new WP_Query($args_featured);
if ($query_featured->have_posts()) :
while ($query_featured->have_posts()) :
$query_featured->the_post();
array_push($featured_post_IDs, get_the_ID());
?>
<?php
endwhile;
endif;
wp_reset_postdata();
return $featured_post_IDs;
} ?>
Then, Use this parameter 'post__not_in' => exclude_featured_post_IDs(), in the second WP_Query loop:
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
**'post__not_in' => exclude_featured_post_IDs(),**
'posts_per_page' => -1,
);
?>

WP Query filter by ACF field don't work

I'm having an issue with WP query filtered by ACF Post Object Field.
I have to query the 'post' filtered by 'author' acf field.
i'm using this code but this don't work
$post_type_query = new WP_Query(
array (
'post_type' => 'post',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'author',
'value' => 'prova'
)
)
)
);
Thereis one article on wordpress post with 'prova' author, but the query return empty.
I can't understand why
Thanks
Try this:
$postData = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 3,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'author',
'value' => 'prova',
'compare' => '=' // or if you want like then use 'compare' => 'LIKE'
)
)
)
);
if($postData->have_posts()):
while ($postData->have_posts()): $postData->the_post();
echo "Post Title";
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;

Show posts from query randomly

I have this query -
<?php if( is_page_template('taxonomy-listing_area-channel.php') ) { ?>
<?php
$posts = get_posts(array(
'post_type' => 'adverts',
'numberposts' => 1,
'order' => 'random',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'associate_adverts',
'value' => '204',
'compare' => 'LIKE',
)
),
));
?>
<?php //if it's returning the object
foreach($posts as $advert){
$img = get_field("top_advert", $advert->ID);?>
<img src="<?php echo $img["url"]; ?>"/>
<?php }?>
But for somr reaosn the posts are just showing as the last one entered and now randomly, I've never had this problem before but I have no idea where I'm going wrong, and help would be much appreciated!
Change here, You have syntax error, use single quotes inside double quotes,
<img src="<?php echo $img['url']; ?>"/>
You need to change this
'post_type' => 'adverts',
'numberposts' => 1,
'order' => 'random',
To
'post_type' => 'adverts',
'posts_per_page' => 1,
'orderby' => 'rand',
Now you code will look like
<?php if( is_page_template('taxonomy-listing_area-channel.php') ) { ?>
<?php
$posts = get_posts(array(
'post_type' => 'adverts',
'posts_per_page' => 1,
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'associate_adverts',
'value' => '204',
'compare' => 'LIKE',
)
),
));
?>
<?php //if it's returning the object
foreach($posts as $advert){
$img = get_field("top_advert", $advert->ID);?>
<img src="<?php echo $img["url"]; ?>"/>
<?php } }?>
Also you forgot to close you if statement.
Thanks for all the help,
It turns out it was a box that needed to be click on WPEngine to allow the random function in a query!

PHP: Delete entry in array of arrays

I need to display "Featured posts" randomly for each day of the week on a single page. Posts need to be current.
What I want to do:
I load 7 posts in a variable using WP_Query, test each post to see if it's current, and display only the remaining ones.
Problem: I can't find how to delete an entry in a while loop.
Init of the page:
$args = array(
'post_type' => 'listing',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'orderby' => 'date',
'order' => 'DESC',
'meta_key' => 'featured_calendar',
'meta_value' => 'on',
'posts_per_page' => 7
);
$featured = new WP_Query($args);
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
$theending = get_post_meta($post->ID, 'theend', true);
if ($theending > time()){
echo "All Good";
} else{
//Delete the entry
}
endwhile;
endif;
Inside the days of the week loop:
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
//Display of remaining posts
endwhile;
endif;
It sounds like you should just modify the query to exclude those posts in the first place:
$args = array(
'post_type' => 'listing',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featured_calendar',
'value' => 'on',
'compare' => '='
),
array(
'key' => 'theend',
'value' => time(),
'compare' => '>',
'type' => 'NUMERIC' // Not sure if you want a TIME here
),
),
'posts_per_page' => 7
);
$featured = new WP_Query($args);
Read more about custom field parameters in the Codex.
If you're looking to remove a row from an array, you can do that like this:
unset ($array[$key]);
$recent_featured = array();
$featured = new WP_Query($args);
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
$theending = get_post_meta($post->ID, 'theend', true);
if ($theending > time()){
$recent_featured[] = $post;
} else{
}
endwhile;
now you can use $recent_featured, pass it, loop over it, display them...

Categories