Query (2) ACF post objects during WP_Query in custom taxonomy template - wordpress

Building a Wordpress page template for a custom taxonomy and related custom post type. Within a new WP_Query I need to grab fields from (2) different ACF post object fields; list-staff and list-rep. Code works as expected up to the wp_reset_postdata(); correct amount of results are returned, data within each post is unique up to the point of the reset. After the reset, all data is the same within each post. Code follows, and I'm certain there's a more elegant solution:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php the_field('list_number'); ?>
<?php
$staff = get_field('list_staff');
$rep = get_field('list_rep');
if( $staff ):
// override $post
global $post;
$post = $staff;
setup_postdata( $post );
?>
<?php the_permalink(); ?><?php the_title(); ?>
<?php
endif;
if( $rep ):
// override $post
$post = $rep;
setup_postdata( $rep );
?>
<?php the_field('mkt_co'); ?><?php the_field('mkt_tel'); ?>
<?php
endif;
wp_reset_postdata();
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>

Figured this one out. setup_postdata() is a completely wrong direction for this application. What is correct is documented on the ACF page for "displaying data for multiple post objects." As the article states "Using this method, the $post object is never changed so all functions need a second parameter of the post ID in question." Read more about it here; https://www.advancedcustomfields.com/resources/post-object/. My working code follows:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php
the_field('list_number');
$post_object = get_field('list_staff');
if( $post_object ):
?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<?php echo get_the_title($post_object->ID); ?>
<?php
endif;
?>
<?php
$post_object = get_field('list_rep');
if( $post_object ):
?>
<p><?php the_field('mkt_co', $post_object->ID); ?></span></p>
<?php the_field('mkt_tel', $post_object->ID); ?>
<?php
endif;
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>

Related

How to order posts on each different category?

In wordpress, right now only on my main blog page, I managed to sort all posts with:
<?php if ( is_active_sidebar( 'blog-category' ) ) : ?>
<?php dynamic_sidebar( 'blog-category' ); ?>
<?php endif; ?>
</div>
<div class="blog_list_content">
<?php
global $wp_query;
$args = array(
'meta_key' => 'publish_date',
'orderby' => 'meta_value',
'order' => 'DESC'
);
$args = array_merge( $wp_query->query, $args );
query_posts( $args );
if (have_posts()) :
while (have_posts()) : the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
theme_paging_nav();
endif;
?>
What do I need to do in order to sort the posts on other categories in the same way?
It is the correct code.
Copy and paste the same login in the single.php file of your theme and you'll get sorting for every category.

Can I get pages from the WP-PostRatings and WP-PostVevs plugins using get_posts()?

I use WP-PostRatings WP-PostViews plugins and was able to display pages using functions
<?php if (function_exists('get_highest_rated')): ?>
<ul><?php get_lowest_rated(); ?></ul>
<?php endif; ?>
<?php if (function_exists('get_most_viewed')): ?>
<ul><?php get_most_viewed(); ?></ul>
<?php endif; ?>
but they are displayed as plain text and I cannot edit them, add a thumbnail of the post and do other things is it possible to not display them as if using get_posts() like this
<?php
global $post;
$args = array(
'post_type' => 'post',
'orderby' => 'comment_count',
'order'=> 'ASC'
);
$myposts = get_posts( $args );
foreach( $myposts as $post ){ setup_postdata($post);?>
<li><?php the_title(); ?></li>
<?php } wp_reset_postdata(); ?>
but only for rating and views

display wp custom posts by catgeory

Added this code to display wp custom post by category, but unable to get pagination to work when added posts_per_page="5"
<?php query_posts('post_type=encounters_news'); while (have_posts()) : the_post(); ?>
<?php $terms = get_the_terms($post->ID, 'encounters_news_categories');
foreach($terms as $item):
if($item->slug == "chapter-news"):?>
<?php get_template_part( 'content-news', 'page' ); ?>
<?php //comments_template( '', true ); ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endwhile; wp_reset_query(); ?>
<?php encounters_content_nav( 'post-nav' ); ?>
Rewrote and change to this code
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$post_type = 'encounters_news';
$tax = 'chapter-news';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$query_args = array(
'post_type' => $post_type,
'$tax' => $tax_term->slug,
'showposts' => 5,
'paged' => $paged
); wp_reset_query();
}}
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php get_template_part( 'content-news', 'page' ); ?>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="navigation">
<div class="alignleft"><?php echo get_previous_posts_link( '« Previous' ); // display newer posts link ?></div>
<div class="alignright"><?php echo get_next_posts_link( 'More »', $the_query->max_num_pages ); // display older posts link ?></div>
This code creates the pagination as desired, but displays all the Encounters News Categories instead of just the Chapter News category.
Suggestions will be greatly appreciated.
Resolved
<?php
$query = new WP_Query( array(
'post_type' => '', // name of post type.
'tax_query' => array(
array(
'taxonomy' => '', // taxonomy name
'field' => '', // term_id, slug or name
'terms' => , // term id, term slug or term name
)
),'showposts' => 5,
'paged'=>$paged
) );
while ( $query->have_posts() ) : $query->the_post();
// do stuff here....
endwhile;
/**
* reset the orignal query
* we should use this to reset wp_query
*/
wp_reset_query();?>

Wordpress query goes to infinite loop

I have only the following code in a Wordpress template file, and though there are only two posts in the database, it goes to infinite loop.
$args = array(
'posts_per_page' => '‐1',
'post_type' => 'products',
);
$myProducts = new WP_Query( $args );
// The Loop
while ( $myProducts->have_posts() ) : $myProducts‐>the_post();
echo 'loop body';
endwhile;
// Reset Post Data
wp_reset_postdata();
If I print the $myProducts variable, I can see the two posts there. Why the infinite loop then?
<?php
$params = array(
'posts_per_page' => 5,
'post_type' => 'product'
);
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>
This works fine for me.

get_the_title() is returning results from previous loop

I'm attempting to loop thought a custom post type (artists), then each category, then another custom post type (story) which has a relationship with the artist.
The issue is, the function <?php echo get_the_title( $story->ID ); ?> in the final loop is returning a title of the Artist many times over, along with the title of the current custom post. I just need the title of the currentent loops post.
<!-- get all the artists -->
<?php
$args = array(
'post_type' => 'artists'
);
$query = new WP_QUERY( $args );
?>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_field('artist_website'); ?></p>
<!-- looping through all the categories -->
<?php
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h2>".$cat->name."</h2>";
?>
<!-- looping through the stories that have a relationship with the artist (select_artist) -->
<?php
$post_type = 'story';
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1, //show all posts
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $cat->slug,
)
),
'meta_query' => array(
array(
'key' => 'select_artist', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123.
'compare' => 'LIKE'
)
)
);
$stories = new WP_Query($args);
?>
<?php if( $stories ): ?>
<ul>
<?php foreach( $stories as $story ): ?>
<!-- Problem echo below -->
<?php echo get_the_title( $story->ID ); ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php } // done the foreach statement ?>
<?php endwhile; wp_reset_postdata(); endif; ?>
</section>
If you're using WP_Query you should be using a while loop and end with the_post() in order to set up the internal variables that would allow get_the_title() to work properly.
As an added measure you could set the $post variable to a $temp variable before your inner loop, then reset back after.
<?php
$stories = new WP_Query($args);
if( $stories->have_posts() ):
global $post;
$temp = $post;
?>
<ul>
<?php
while( $stories->have_posts() ): $stories->the_post();
the_title();
endwhile;
$post = $temp;
?>
<ul>
<?php endif; ?>

Resources