Nested foreach loop not functioning correctly. - wp - php

I have custom post types 'stockist' and 'product', the latter having a taxonomy 'range'. Products are assigned to stockists using an ACF Post Object.
I'm trying to loop through 'stockist', return the 'range' term from the post object and then list the products for each term. So far I can loop all stockists and return all range terms however the foreach loop that returns the product list doesn't appear to be looping as it only returns the first set of values for each stockist.
Can anyone see where I'm going wrong?
<?php
$args2 = array( 'post_type' => 'stockist', 'posts_per_page' => -1 );
$stockistloop2 = new WP_Query( $args2 );
if ( $stockistloop2->have_posts() ): while ( $stockistloop2->have_posts() ): $stockistloop2->the_post();?>
<div class="col-1-1 clearfix nopad stockist-block-dropdown <?php the_title();?>-block">
<?php
$args = array( 'taxonomy' => 'range', 'hide_empty' => 0);
$categories = get_categories($args);
if($categories): foreach($categories as $category): $url = get_category_link( $category->term_id ); ?>
<div class="col-1-5">
<h4>
<?php echo ($category->name) ;?>
</h4>
<ul class="stockist-block-products clearfix">
<?php $post_objects = get_field('stocked_range'); if( $post_objects ):
foreach( $post_objects as $post): $post_terms_array = get_the_terms($post, 'range'); $post_term_name = $post_terms_array[0]->slug;
setup_postdata($post);
if($post_term_name == $category->slug):?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endif;
endforeach;
endif;?>
</ul>
</div>
<?php endforeach; ?>
<?php endif;?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
UPDATE
Replacing the inner loop with the following has resolved the issue:
<?php $post_objects = get_field('stocked_range');
if( $post_objects ): ?>
<ul class="stockist-block-products clearfix">
<?php foreach( $post_objects as $post_object): $post_terms_array = get_the_terms($post_object, 'range'); $post_term_name = $post_terms_array[0]->slug;
if($post_term_name == $category->slug):?>
<li>
<?php echo get_the_title($post_object->ID); ?>
<span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span>
</li>
<?php endif; endforeach; ?>
</ul>
<?php endif;?>

Related

post time don't change in loop - wordpress

I want a simple loop to get latest five posts, which only consist of post title and time i wrote the loop below and the title generates fine, however the time doesn't change. as it get the first post in the loop's time for other posts as well. so all the post time is same.
Please advise why time don't loop ?
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){ ?>
<li class="orbit-slide">
<div>
<a href="<?php echo get_permalink($recent["ID"]); ?>" class="ticker-a">
<span><?php echo get_the_time($recent["g:i a"]); ?> </span>
<?php echo $recent["post_title"]; ?>
</a>
</div>
</li>
<?php }
wp_reset_query();
?>
Try this one
echo get_the_time('', $recent["ID"]);
On a side note. This is a much more efficient piece of code and it is easier to output content.
<?php
$args = array( 'posts_per_page' => '5', 'orderby' => 'most_recent' );
$recent_posts = new WP_Query( $args );
if ( $recent_posts->have_posts() ) : while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="orbit-slide">
<div>
<a href="<?php the_permalink(); ?>" class="ticker-a">
<span><?php the_time("g:i a"); ?> </span>
<?php the_title(); ?>
</a>
</div>
</li>
<?php
endwhile; endif;
wp_reset_query();
?>
Thanks "Aron" for answer 1 :
echo get_the_time('', $recent["ID"]);
And Thanks "WizardCoder" for the other loop solution:
<?php
$args = array( 'posts_per_page' => '5', 'orderby' => 'most_recent' );
$recent_posts = new WP_Query( $args );
if ( $recent_posts->have_posts() ) : while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="orbit-slide">
<div>
<a href="<?php the_permalink(); ?>" class="ticker-a">
<span><?php the_time("g:i a"); ?> </span>
<?php the_title(); ?>
</a>
</div>
</li>
<?php
endwhile; endif;
wp_reset_query();
?>

exclude category from wp_count_posts()

I am trying to exclude a category from wp_count_posts(). I've found hundreds of posts that explain how to do a WP_Query for a specific post type and get a category and then exclude posts with specific terms or tags.
However not very publicized is how to to exclude a few categories from a specific post type. Once I do that I need to return a post count as well
See below what I am working with. Looking for an alternative method for doing what I have below that will allow for me to exclude 3 specific categories.
<?php
$count_posts = wp_count_posts('sponsors');
$published_posts = $count_posts->publish;
$count = $published_posts/7;
$args = array(
'posts_per_page' => -1,
'post_type' => 'sponsors'
);
query_posts($args);
?>
<?php if( have_posts() ) : $i = 0; $f =7; $countr=0; $z = 0;?>
<div class="gallery-sponsors">
<strong class="title">Special Thanks To Our Sponsors</strong>
<div class="mask">
<div class="slideset">
<?php for ($k = 1; $k <= $count; $k++):
$countr = $countr+$f;?>
<?php if ($k==1):?>
<?php else:?>
<?php $i = $i+$f;?>
<?php endif;?>
<div class="slide">
<ul class="sponsors-list">
<?php $z=0;?>
<?php while (have_posts()) : the_post(); ?>
<?php $z++; if ($z<=$countr and $z>$i): ?>
<li>
<div class="img-hold">
<?php the_post_thumbnail( 'sponsors-gallery-home' ); ?><?php echo $z;?>
</div>
</li>
<?php endif;?>
<?php endwhile; ?>
</ul>
</div>
<?php endfor;?>
</div>
</div>
<nav class="pagination"><?php $k =0;?>
<ul>
<?php while ($q<$count-1): $q++;?>
<?php if ($q==1):?>
<li class="active"><?php echo $q;?></li>
<?php else:?>
<li><?php echo $q;?></li>
<?php endif;?>
<?php endwhile;?>
</ul>
</nav>
</div>
<?php endif;?>
<?php wp_reset_query(); ?>
Here's the fix. I had to add the following to the $args.
'cat' => '-71,-72,-73'
Pretty simple but these very little documentation on how to do this with wp_count_posts(). Here was my complete solution.
<?php
$count_posts = wp_count_posts('sponsors');
$published_posts = $count_posts->publish;
$count = $published_posts/7;
$args = array(
'posts_per_page' => -1,
'post_type' => 'sponsors',
'cat' => '-71,-72,-73'
);
query_posts($args);
?>

Post Objects in Advanced Custom Fields order by date

Hi I have a Post Object field in Advanced Custom Fields that I want to return multiple posts, ordered by date. I have the custom field data from those posts returning fine, but the Post Objects return in order of the Post ID. I want them to be ordered by the date that the post was published.
<?php $post_objects = get_field('exhibitions');
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>
</div>
</a>
<br><br>
<?php endforeach; ?>
<?php endif; ?>
This returns the text custom fields 'title' and 'dates' from each post thats selected in the Post Objects field on the post where this is called.
I want the posts to return here by order of their publish date.
Any ideas?
#Michael Ray-Von - your answer worked, but it involved getting the same data from the db twice. Instead you can just sort the post data returned in your initial ACF query rather than running the extra query. (The post_date is returned as a string so you can strcmp it):
<?php
// get the posts from ACF
$custom_posts = get_field('your_posts_field');
// sort the posts by post date, but you can also sort on ID or whatever
usort($custom_posts, function($a, $b) {
return strcmp($b->post_date,$a->post_date);
});
// write them out
foreach ($custom_posts as $post) : setup_postdata($post); ?>
<article>
<h1><?php the_title();?></h1>
<?php the_excerpt(); ?>
</article>
<?php
endforeach;
wp_reset_query();
?>
Hat-tip to this answer for the sorting: https://stackoverflow.com/a/10159521
Okay i've got it figured out!
Instead of calling get_field as the post_objects, you call it as a variable just to get the IDs of relevant posts, and then use that in an array for the $args of a get_posts. That way you have access to all the array options of get_posts before running the loop.
<?php
$ids = get_field('exhibitions', false, false);
$args = array(
'post__in' => $ids,
'orderby' => 'post_date',
);
$post_objects = get_posts( $args );
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>
</div>
</a>
<br><br>
<?php endforeach; ?>
<?php endif; ?>
Thanks for your help!
found my answer thanks to: http://support.advancedcustomfields.com/discussion/5846/adding-args-to-post_objects-get_field/p1
<?php $post_objects = get_field('exhibitions');
$args = array (
'orderby'=>'date',
);
$the_query = new WP_Query( $args );
if($the_query->have_posts()) {
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>` </div>
</a>
<br><br>
endwhile;
wp_reset_postdata();
}
I haven't tested, But it should work for you with little adaption !

Limiting posts in loop

Hi I have a loop with testimonials using advanced custom fields. I need the loop to only loop one post at a time randomly i have tried query_posts but its doest work.
<?php
query_posts( 'posts_per_page=1&orderby=rand' );
if(get_field('testimonials', 'options')): ?>
<?php while(has_sub_field('testimonials', 'options')): ?>
<ul>
<li class="title"><?php the_sub_field('name'); ?></li>
<li class="site"><?php the_sub_field('website'); ?></li>
<li class="desc"><?php the_sub_field('message'); ?></li>
</ul>
<?php endwhile; ?>
<?php endif; ?>
There's a problem with the while loop, you should do it like this:
<?php
$posts = new WP_Query();
$posts->query('posts_per_page=1&orderby=rand');
if (have_posts()) :
while (posts->have_posts()) : $posts->the_post();
if(get_field('testimonials', 'options')): //Ain't no sure what does this ?>
<ul>
<li class="title"><?php the_sub_field('title'); ?></li>
<li class="site"><a href="<?php the_sub_field('website'); ?>" target="_blank">
<?php the_sub_field('website'); ?></a></li>
<li class="desc"><?php the_sub_field('message'); ?></li>
</ul>
<?php
endif;
break; // Exit loop after first post
endwhile;
endif;
?>
Look how i'm using the while loop. I don't understand what get_field does, you should pass the post ID as second parameter.
Try this for looping out one post per page:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand'
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<ul>';
echo '<li>' . get_the_title() . '</li>';
echo '</ul>';
echo '<li class="title">'.the_sub_field('name'). '</li>';
echo '<li class="site">'.the_sub_field('website').'</li>';
echo '<li class="desc">'.the_sub_field('message').'</li>';
endwhile;
wp_reset_postdata();
I found the solution here :)
http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

wordpress related posts by category duplicating

I am having an issue getting the $do_not_duplicate working properly I have several title duplicating on my blog and i need it to stop. Here is what i have so far:
<?php if (is_single()): ?>
<section>
<h3>Related Posts</h3>
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
$do_not_duplicate[] = $post->ID;
if ( count ( $cats ) > 0):
$args = array( 'numberposts' => 3, 'category' => implode($cats, ","), 'exclude' => $post->ID, 'post__not_in' => $do_not_duplicate );
$related_posts = get_posts( $args );
if (count($related_posts)): ?>
<ul>
<?php foreach ($related_posts as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>"><?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; if ( get_the_title() ) the_title(); else the_ID(); ?><?php
endwhile;
wp_reset_query(); ?>
</a></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No related posts found.</p>
<?php endif; ?>
<?php else: ?>
<p>No related posts found.</p>
<?php endif; ?>
</section>
<?php endif; ?>
you have a while (have_posts() ) within a foreach and this produces the duplication. You may change your loop to something like this:
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
$do_not_duplicate[] = $post->ID;
if ( count ( $cats ) > 0):
$args2 = array( 'numberposts' => 3, 'category' => implode($cats, ","), 'exclude' => $post->ID, 'post__not_in' => $do_not_duplicate );
$related_posts = get_posts( $args2 );
if (count($related_posts)):
?>
<ul>
<?php foreach ($related_posts as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>" ><?php $do_not_duplicate[] = $post->ID; if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php wp_reset_query(); ?>
<?php endif;endif; ?>
At the end of the loop, the var $do_not_duplicate save the id of the post and all the id of the relative posts.

Categories