I need 2 posts, but, only shows 1 - WP_Query - wordpress

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

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.

Target custom field type plugin value using WP_Query

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'
);

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 page.php not show posts

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.

Wordpress - Sorting post loop by meta data date

I have two CPTs, one called 'artist' and the other called 'release.' I've created a single-artist.php page that displays an artist and its' custom meta data. On that same page I am displaying all releases by that artist with the following code:
<!-- GET RELEASES -->
<?php
$category = get_the_category();
$artist_name_slug = $category[0]->slug;
$args = array ('post_type' => 'release', 'posts_per_page' => 20, 'category_name' => $artist_name_slug);
query_posts ($args);
?>
<?php if (have_posts()) : ?>
<h3 class="artist-col2-title">Releases</h3>
<?php while (have_posts()) : the_post(); ?>
<div class="artist-release"><?php echo the_post_thumbnail('small'); ?></div>
<?php endwhile; ?>
<?php endif; ?>
<div style="clear:both;"></div>
Within the release CPT I have a release date in the meta data.
I would like to sort the releases based on that date but I cannot figure out how to add that to my arguments. Any help would be greatly appreciated!
To sort by meta data you can use
$args = array (
'post_type' => 'release',
'posts_per_page' => 20,
'category_name' => $artist_name_slug,
'meta_key' => 'your_meta_key' // i.e. release_date
'orderby'='meta_value' // for numeric value use 'meta_value_num' instead
);
query_posts ($args);
But notice the meta_key should be present in the query that you want to use for sorting, see for more.

Resources