If I add to sidebar this code I can't display anything in page.php I try reset query but no result.
<?php
$args = array(
'post_type' => 'post',
'kalba' => 'lt',
'posts_per_page' => 7
);
$new_query = new WP_Query();
$new_query->query($args);
?>
<?php if ($new_query->have_posts()) : ?>
<?php while ($new_query->have_posts()) : the_post(); ?>
<?php $new_query->the_post();?>
<?php the_title(); ?>
<div class="sidebar-line"></div>
<?php endwhile; ?>
<?php endif; ?>
got confuse y you are using
'kalba' => 'lt',
in your argument. wp_query have no any such argument 'kalba'
may be you can try and change your argument lik
$args = array(
'post_type' => 'post',
'posts_per_page' => 7
);
like this
.. hope it works fine
Before loop I add <?php rewind_posts(); ?> and when it work.
Related
I'm using this wp_query. I want to show 2 posts on my sidebar, but, it is showing only one - the loop have any wrong config? thank you!
<?php
$destaque = new WP_Query('post_type=post&posts_per_page=1&cat=2,3,4,5');
if($destaque->have_posts()):
while($destaque->have_posts()):
$destaque->the_post();
?>
<div class="col-md-12">
<?php get_template_part('content','homepost'); ?>
</div>
<?php
endwhile; wp_reset_postdata();
endif;
?>
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'category__not_in' => array(1),
'category__in' => array(2,3,4,5),
'offset' => 1
);
$secundarias = new WP_Query($args);
if($secundarias->have_posts()):
while($secundarias->have_posts()):
$secundarias->the_post();
?>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
Don t know why you have two queries, but it would simply be
$new_query = new WP_Query();
$new_query->query('post_type=post&showposts=2');
or in your example
('posts_per_page=2');
On your first query you set that to "1".
Use a single query with that before your loop and it should work.
Also check your categories - sometimes it is just
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();
?>
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.
In wordpress, using the CTU plugin, I made a custom post type called apartments with a field called available. I only want to target any post with available=yes. How would I do this? I tried the_post('available=yes')
<?php
$args = array(
'post_type' => 'apartment'
);
$the_query = new WP_Query( $args );
?>
<?php if( have_posts()): ?>
<?php while ($the_query->have_posts()): ?>
<?php $the_query->the_post(); ?>
<?php $the_field('available'); ?>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
i think the simplest solution is:
1. In your custom taxonomy create a term called available
2. add this to your custom query
$args = array(
'post_type' => 'apartment',
'category_name' => 'available'
);
I have a custom post type named "audio". To display all the posts in a page I wrote the code below, but it does not work.
<?php
$post_type = "audioes";//post type names
echo "djnbfj";
$post_type->the_post();
// The Query
$the_query = new WP_Query( array(
'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
'showposts' => 1,
));
// The Loop
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="issue-content">
<?php get_template_part('includes/breadcrumbs', 'page'); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();}
get_template_part('loop-audio', 'page');
?>
</div><!--issue-content-->
<?php endwhile; ?>
</div> <!-- end #left_area -->
How can i got the posts type i want the custom one written above.
Is the post type called "audio" or "audioes"? Your $post_type variable is set to "audioes".
I'm not sure what the get_template_part lines or the echo line is doing, but the below code should display your post type. You also have the post type name as "audioes" in your code, but you state that the name is "audio". Try the code below, but if the post type is named "audioes" then you'll need to change that in my code below. You can wrap additional divs, spans, or other HTML tags around <?php the_post_thumbnail(); ?> and <?php the_content(); ?> to give them CSS styles is you wish to.
<?php query_posts(array('post_type' => 'audio', 'posts_per_page' => 1, 'orderby' => 'post_date', 'order' => 'DESC')); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="issue-content">
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); ?>