Query Page with Posts In WordPress - php

I am pulling posts from a specific category. This feature works. However, I also need it to pull pages every now and then and I can't figure out a way to get it to pull this in addition to the posts.
HTML/PHP
<?php query_posts('cat=8&posts_per_page=3'); while (have_posts()) : the_post(); ?>
<div>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="card-img img-responsive">
<p class="feature-card-head"><?php the_title(); ?></p>
<p class="feature-card-txt"><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I'd like to keep this structure and find a way to include pages, any feedback is helpful!

You cannot fetch posts and use this query as it is. WordPress posts have no categories by default so the cat=x part will always exclude automatically all posts.
I think there is no better solution than using a second query. Depending on what you are using this for, it may be better to separate these loops.
If you want to use a single loop for multiple queries consider merging the query like mentioned here:
<?php
$query1 = new WP_Query(array(
'cat' => 8,
'post_type' => 'post',
'posts_per_page' => 3
));
$query2 = new WP_Query(array(
'post_type' => 'page',
));
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );
$wp_query->post_count = $query1->post_count + $query2->post_count;
?>
<?php while( $wp_query->have_posts() ): $wp_query->the_post(); ?>
<div>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="card-img img-responsive">
<p class="feature-card-head"><?php the_title(); ?></p>
<p class="feature-card-txt"><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>

Related

Proper get_post blog php - safest way

What is the proper, safe way to cull blog posts? I'm just being cautious before I code it on my ecommerce site.
On my personal site I've found this to work just fine.
<?php
$args = array(
'numberposts' => 12,
'offset' => 1
);
$latest_post = get_posts( $args ); ?>
<?php foreach( $latest_post as $post ) : setup_postdata( $post ); ?>
<div class="col-sm-12 col-md-6 col-lg-4 waypoint_card">
<a href="<?php echo esc_url( get_permalink() ); ?>">
<div class="waypoint_img" style="background-image:url('<?php echo get_the_post_thumbnail_url(); ?>');"></div>
<div class="waypoint_content">
<h3><?php echo the_title(); ?></h3>
</div>
</a>
</div>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
I end up doubling this $args array so I can separate the first post from the rest (offset 1). Is there a better way to do this?
It's legit, if your purpose is to get the first 12 posts minus the first.

How can I use query_posts to filter posts from a specific year?

How can I use the query_posts functions within Wordpress to filter my posts out by a specific year?
Here's my current code:
<?php query_posts('cat=3&posts_per_page=10');
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<a href="<?php the_permalink(); ?>">
<div class="testpost">
<?php the_post_thumbnail(); ?>
<h4><?php the_title(); ?></h4>
<hr class="smallblue">
<h5><?php the_time(); ?></h5>
<h6><?php echo get_the_date(); ?></h6>
</div>
</a>
<?php
endwhile; endif; >
wp_reset_query();
?>
You can use the date_query argument of WP_Query, which has the advantage of being slightly more readable:
$args = array(
'cat' => 3,
'posts_per_page' => 10,
'date_query' => array(
array(
'year' => 2017,
),
),
);
$myQuery = new WP_Query($args);
// Or $myQuery = get_posts($args);
if ($myQuery->have_posts()) :
while ($myQuery->have_posts()) : $myQuery->the_post();
// Output
endwhile;
endif;
But please: never, ever use query_posts. It should only ever be used by the core. Use one of the many, many other functions available like WP_Query or get_posts, where the same array of arguments can be used safely.
<?php query_posts('cat=3&posts_per_page=10&year=2004'); ?>
pass the year to the query

Wordpress Get Posts not working as expected

I'm trying to display 6 posts from a specific custom post type in Wordpress. Everything is working, except for when I remove the line "$wp_query = new WP_Query();". When missing that line, 20 posts gets displayed in alphabetical order and I have no idea why. wp_reset_postdata() or wp_reset_query() don't seem to do anything.
<?php $wp_query = new WP_Query( ); ?>
<div class="blog-footer row margin-top">
<?php
global $post;
$args = array( 'post_type' => 'blog', 'posts_per_page' => 6 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-2 col-sm-4">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Happy to get any inputs on why this behaves the way it does. Appreciate it!
global $post is a global variable mostly used by wordpress itself and is used to get contents/ attributes of current post or page mostly..
Read details here
https://codex.wordpress.org/Function_Reference/$post
try below code.
<div class="blog-footer row margin-top">
<?php
wp_reset_query() ;
$args = array( 'post_type' => 'blog', 'posts_per_page' => 6 );
$myposts = get_posts( $args );
foreach ( $myposts as $b_post ) { ?>
<div class="col-md-2 col-sm-4">
<a href="<?php $b_post->post_name; ?>">
<?php
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $b_post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0] ?>"/>
<h4><?php $b_post->post_title; ?></h4>
</a>
</div>
<?php }
wp_reset_postdata();?>
</div>

Exclude current post from loop

I want to add a Wordpress loop for a specific category in a post template that exculdes the current post.
I was suggested to use:
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
But I'm having trouble getting it to work.
My loops currently looks like this.
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Try this code.
$postid = get_the_ID();
$args=array(
'post__not_in'=> array($postid),
'post_type' => 'post',
'category_name'=>'video',
'post_status' => 'publish',
'posts_per_page' => 4
);
<div class="video">
<?php
$catquery = new WP_Query( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Use
'post__not_in' => array($post->ID)
The two code blocks are using two different techniques for a Wordpress custom loop... the first modifies the global query, and the second creates a new custom query. I've outlined both below with your loop template.
Example with suggested code, global query:
Loop through the global $wp_query object in the loop code:
<div class="video">
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
<!-- use the global loop here -->
<?php while ( have_posts() ) : the_post(); ?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p
</div>
Example with original code, custom query:
Loop through the custom query, adding 'post__not_in':
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4&post__not_in=' . $post->ID );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Sorry if my original answer was unclear, I initially thought you were combining the two code blocks.

Display featured posts from a specific WordPress category

I have some code written which displays the latest 5 posts from a specific category however I can't work out how to make it display the latest 5 posts from that category that are marked as featured only. By featured I mean the post has been stickied, so basically it will display the 5 posts from each category that have been stickied.
<ul id="index-blog">
<?php $the_query = new WP_Query( 'category_name=whats-on&showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="index-thumb"><?php the_post_thumbnail(array(50,50), array ('class' => 'alignleft')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<li>
<?php the_excerpt(__('(more…)')); ?>
</li>
<?php endwhile;?>
</ul>
Try this:
$sticky=get_option('sticky_posts');
$query_args=array(
'post__in' => $sticky,
'category__in'=>array($category)
);
$the_query = new WP_Query($query_args);
You can get the top 5 sticky posts using rsort and array_slice as shown in http://codex.wordpress.org/Sticky_Posts
The issue with the other answer is that introduces a variable - $category - that you have to first populate.
Here's the revised code, including how to populate the variable:
<ul id="index-blog">
<?php $category_id = get_cat_ID( 'whats-on' );
$args = array(
'cat' => $category_id,
'post__in' => get_option('sticky_posts'),
);
?>
<?php $the_query = new WP_Query($args); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// Fix some markup issues here - the children of `ul` elements must be `li` elements....
<li>
<div class="index-thumb"><?php the_post_thumbnail(array(50,50), array ('class' => 'alignleft')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<div class="excerpt">
<?php the_excerpt(__('(more…)')); ?>
</div>
</li>
<?php endwhile;?>
</ul>

Categories