Break up posts per page in two sections - php

I have a custom post type called 'videos' and I am using the Advanced Custom Fields plugin with a select field to use as a filter to assign where the post part will display on my page. I have two columns, one called "our work' and one called 'featured films'. I need each section to display the latest 4 posts but when I change posts_per_page it affects the total number, is there a way to limit it to just 4 per query? Sub question is it ok to have the same query run two times? My code is :
<div class="triple ourWork col-sm-6">
<h2>Our Work</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'videos', 'posts_per_page'
=> 4) ) ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); /* start the loop */ ?>
<?php if( get_field('labeled_as') == 'our work' ): ?>
<div class="col-sm-6" id="">
<?php global $post;
$gethref = $post->post_name;
?>
<div class="holder" style="background-image: linear-gradient(0deg,rgb(38, 38, 42, .5),rgb(38, 38, 42, .5)), url(<?php echo the_field('screenshot'); ?>);"><span class="play"><?php the_title(); ?></span></div>
<p><?php echo the_field('issue_short_description'); ?></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
<div class="triple featuredFilms col-sm-6">
<h2>Featured Films</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'videos', 'posts_per_page' => 4 ) ) ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); /* start the loop */ ?>
<?php if( get_field('labeled_as') == 'featured film' ): ?>
<div class="col-sm-6" id="">
<?php global $post;
$gethref = $post->post_name;
?>
<div class="holder" style="background-image: linear-gradient(0deg,rgb(38, 38, 42, .5),rgb(38, 38, 42, .5)), url(<?php echo the_field('screenshot'); ?>);"><span class="play"><?php the_title(); ?></span></div>
<p><?php echo the_field('issue_short_description'); ?></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>

You want to use meta queries for acf fields. https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
I personally would use 2 variables for the 2 loops for debugging, etc.
$work_loop = new WP_Query(
array(
'post_type' => 'videos',
'posts_per_page' => 4,
'meta_key' => 'labeled_as',
'meta_value' => 'our work'
)
);
This will fetch only posts that have that label. The way you are doing it currently, you get 4 results then filter for the label. So if 3 are in the other label, you will only show 1 result. This gets you 4 that will show. Just change the meta value for the second loop and you should be good to go.

Related

Show recent blog posts, but exclude current post

I want to display the 3 most recent posts at the bottom of my single-blog page, but without the current posts.
my code:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<?php esc_html_e('alle blogberichten', 'dfib-theme'); ?>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
lees meer
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
It displays 3 posts, but when I visit the most recent post, the same post is displayed at the bottom again. Is there a way to exclude the current one every time?
I just found the solution on this website: https://pineco.de/snippets/exclude-current-post-from-wp_query/
I just added this piece to my query:
'post__not_in' => array(get_the_ID())
You have to exclude the current post with the post__not_in.
Add post__not_in in WP_Query array like below.
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>

Custom Post Type posts not being displayed for specified category id

I want to display all posts for a specific custom post type with a specific category. Right now, all posts from the specified post type are showing, but none show once I add a category id to the $args array. I am trying to get the Feature category to display, id shown in backend url is 3156. I currently have three posts in the Feature category (they are displaying correctly on the loop-category page).
<?php
$args = array(
'post_type' => 'technology_articles',
'cat' => 3156,
'posts_per_page' => -1
);
$my_query = null;
$my_query = new WP_Query($args);
?>
<?php if( $my_query->have_posts() ):?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div style="background: url('<?php echo get_the_post_thumbnail_url(); ?>');background-size:cover;background-position: center;">
<a class="featured--link" href="<?php echo the_permalink(); ?>"></a>
<div class="slide-overlay">
<div class="category-tag">
Feature
</div>
<h2><?php echo the_title(); ?></h2>
<div class="posted">
Posted on <?php the_time('M j, Y'); ?> by <?php the_author_posts_link(); ?>
</div>
<div class="clear"></div>
</div>
</div>
<?php endwhile; ?>
<?php
endif;
wp_reset_query();
?>
I needed to use tax_query. See below:
<?php
$args = array(
'post_type' => 'technology_articles',
'tax_query' => array(
array(
'taxonomy' => 'technology_category',
'terms' => 3156
)
),
'posts_per_page' => -1
);
$my_query = null;
$my_query = new WP_Query($args);
?>

Loop inside content-single

I try to build an option for the user to highlight "related posts" to be shown at the bottom of a single post, so I put this code at the bottom of "content-single.php":
<div class="related-post">
<?php
$args = array( 'post__in' => array(38328, 38359, 21321) );//it's going to be the user choice, for example, I put three id's
$related_query = new WP_Query($args);
if ($related_query):
echo '<div class="row">';
while ($related_query->have_posts()): $related_query->the_post();?>
<div class="col-sm-4">
<?php
if (has_post_thumbnail()):
the_post_thumbnail();
endif;
?>
<?php the_title() . '<br/>'; ?>
</div>
<?php endwhile;
echo '</div>';
wp_reset_postdata();
?>
</div>
But in some posts, I get 8 diefferent posts, in some posts I don't get any posts (though I put only three id's in post__in).
I guess the problem is that I try to loop inside the main loop.
I tried with query_posts and with get_posts, doesn't work.
Any help will be appreciate!
I think that is related to the missing post_type argument. Yet you need some corrections in your loop structure:
<div class="related-post">
<?php
$args = array( 'post_type' => 'your_post_type', 'post__in' => array(38328, 38359, 21321) );
$related_query = new WP_Query($args);
if( $related_query->have_posts() ){ ?>
<div class="row"><?php
while( $related_query->have_posts() ){ $related_query->the_post();?>
<div class="col-sm-4"><?php
if( has_post_thumbnail() ) the_post_thumbnail();
the_title();?>
</div><?php
}
wp_reset_postdata();?>
</div><?php
}?>
</div>
UPDATE
Please try to add 'posts_per_page' => 3 to the args array.
I have a solution to this post. Hope so you are wrong with the query post.
<div class="related-post">
<?php
$args = array(
'post_type' => 'your_post_type',
'post__in' => array(38328, 38359, 21321),
'post_status'=>'publish'
);
$related_query = new WP_Query( $args );
?>
<?php if ( $related_query->have_posts() ) : ?>
<div class="row">
<?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
<div class="col-sm-4">
<!-- do stuff ... -->
the_post_thumbnail();
the_title();
</div>
<?php endwhile; ?>
</div>
<?php endif; wp_reset_postdata(); ?>
</div>
Hope so it helps you to fetch out your Output Required.
Make sure you have post in the ID that you have mentioned in the post_in section.
Well, I manage to solve this by adding this 'ignore_sticky_posts' => 1 to the arguments. thanks #Pieter Goosen

Post Type Error

I added a code in my php and i don't know what is wrong, it shows only 2 posts but I got 10 posts in my WordPress can someone check my code?
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'appetizers',
'post_status' => 'publish'
);
$Loop = new WP_Query( $args );
?>
<div class="row">
<?php while ( $Loop->have_posts() ) : $Loop->the_post();?>
<a href="<? the_permalink(); ?>">
<div class="col-sm-3">
<?php the_post_thumbnail(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php the_field('price'); ?>
</div>
</a>
<?php endwhile; ?>
</div>
add 'posts_per_page' => -1 in $args.
May be it is picking up the number of posts from the 'Setting -> Reading' option in back end.

How to retrieve Posts in WordPress into a custom HTML Table?

My question is How can I create two rows with 2 columns <td> in a Dynamic way, receiving posts from the query_post()!
I spent sometime using the query_post() in WordPress to create this :
I made a drastic mistake and this is the snippet:
http://pastebin.com/cp6RSTQQ
this gives me the recent posts of a category called watch using the offset in the query_post()
in this way:
<?php
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC','category' => 'watch', 'orderby' => 'post_date','offset' => 0 );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<td class="leftBoxes">
<div class="imgMargin"> <?php the_post_thumbnail(); ?> </div>
<br>
<div class="boxScrollsBlogsView">
<h2><?php the_title(); ?> </h2>
<P>
<?php the_excerpt(); ?>
</P>
<h3> ReadMore</h3>
</div>
</td>
<?php endforeach; ?>
After I wanted to go forward, I realized that I can't do pagination in the way I wrote th code!
I have looked at this QUESTION on wordpress website, but I can't understand how he is doing it honestly!
Just deleted the the right box and I changed the code to look like this
<?php
$args = array( 'posts_per_page' => 4, 'order'=> 'DESC','category' => 'watch', 'orderby' => 'post_date','offset' => 0 );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<td class="leftBoxes">
<div class="imgMargin"> <?php the_post_thumbnail(); ?> </div>
<br>
<div class="boxScrollsBlogsView">
<h2><?php the_title(); ?> </h2>
<P>
<?php the_excerpt(); ?>
</P>
<h3> ReadMore</h3>
</div>
</td>
<?php endforeach; ?>
and it creates one TD after another eventually it goes to the new line for making rows

Categories