WP_Query

Posted on: October 27th, 2022
By: Tadeo Martinez

$homepageServices = new WP_Query(array(
    'posts_per_page'=>3,
    'post_parent'=>$post->ID,
    'post_type'=>'posts',
    'category_name'=>'politics',
    'post__not_in' => [get_the_ID()],
    'date_query' => array(
        array(
          'after' => 'Today',
          'inclusive' => true,
          )
        ),
));
while($homepageServices->have_posts()){
    $homepageServices->the_post();

} wp_reset_postdata(); 

Exclude Posts

$homepageProducts = new WP_Query(array(
    'posts_per_page' => 4,
    'post_type'=>'product',
    'post__not_in' => array(524),
    'meta_key'=>'total_sales',
    'orderby'=>'meta_value_num'
));
while($homepageProducts->have_posts()){
    $homepageProducts->the_post();

} wp_reset_postdata(); 

Filter by Category (Dynamically)

<?php 
$category = get_the_category($post->ID); 
$catid = $category[0]->cat_ID;
$catName = $category[0]->cat_name;
?>
<div class="card p-3 mt-2 mb-2">
<h3 class="teko">Similar Posts</h3>
<?php
$recentBlog = new WP_Query(array(
'posts_per_page' => 5,
'post_type' => 'post',
'cat' => $catid
)); ?>
<ul>
<?php while($recentBlog->have_posts()){
$recentBlog->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } wp_reset_postdata(); ?>
</ul>
</div>

Check if Query Has an Even Amount of Items

https://wordpress.stackexchange.com/questions/113185/how-to-get-a-different-html-for-odd-even-posts

<?php
$counter = 0;
$homepageServices = new WP_Query(array(
'posts_per_page' => -1,
'post_parent'    => 23,
'post_type' => 'page'
));

while($homepageServices->have_posts()) {
$homepageServices->the_post();
$counter++; 
if($counter % 2 == 0): ?>
<div class="col-md-6 p-0 services-col mt-2 mb-2 even-amount">
<a href="<?php the_permalink(); ?>">
<div class="hover-fade position-absolute"></div>
<div class="inner-content position-relative">
<?php the_post_thumbnail('full', array('class'=>'w-100 services-img')); ?>
<div class="inner-content-text position-absolute text-white">
<h3 class="text-uppercase mb-4 text-shadow heading"><?php the_title(); ?></h3>
<!-- <a href="<?php the_permalink(); ?>" class="view-services btn btn-secondary text-uppercase">View Services</a> -->
</div>
</div>
</a>
</div> <!-- end of col -->

<?php else: ?>
    <div class="col-md-6 p-0 services-col mt-2 mb-2 odd-amount">
<a href="<?php the_permalink(); ?>">
<div class="hover-fade position-absolute"></div>
<div class="inner-content position-relative">
<?php the_post_thumbnail('full', array('class'=>'w-100 services-img')); ?>
<div class="inner-content-text position-absolute text-white">
<h3 class="text-uppercase mb-4 text-shadow heading"><?php the_title(); ?></h3>
<!-- <a href="<?php the_permalink(); ?>" class="view-services btn btn-secondary text-uppercase">View Services</a> -->
</div>
</div>
</a>
</div> <!-- end of col -->

<?php endif; ?>

<?php } wp_reset_postdata(); ?>

Show Top Sellers in WooCommerce

Quick Guide on Filtering: Using WP_Query to pull and display WooCommerce Products

<?php
$homepageProducts = new WP_Query(array(
    'posts_per_page' => 4,
    'post_type'=>'product',
    'meta_key'=>'total_sales',
    'orderby'=>'meta_value_num'
));
while($homepageProducts->have_posts()){
    $homepageProducts->the_post(); ?>
<div class="col-md-3">
<div class="content position-relative">
<?php the_post_thumbnail('full',array('class'=>'w-100 h-100')); ?>
<h3><?php the_title(); ?></h3>
</div>
</div>
<?php } wp_reset_postdata(); ?>

How to display the price

echo get_post_meta( get_the_ID(), '_regular_price', true ); 
echo get_post_meta( get_the_ID(), '_sale_price', true );

Shorter version

echo $product->get_price_html();

Filter by Child Pages

global $post;
$industries = new WP_Query(array(
'posts_per_page' => -1,
'post_parent'    => $post->ID,
'post_type' => 'page'
));

while($industries->have_posts()) {
$industries->the_post();

} wp_reset_postdata();

To Filter Pages/Posts by Categories

'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'main',
),),

Where status is the category and terms is the slug of the desired category.  Field needs to be exact as shown in code, no change needed. The methods below might be obsolete but I’ll leave them for future reference.

For Main Category Pages

$cats = get_the_category();
$categoryPosts = new WP_Query(array(
'posts_per_page' => -1,
'cat' => $cats[0]->term_id,
));

while($categoryPosts->have_posts()) {
$categoryPosts->the_post(); 

How to Filter Categories Itself

$blog = array(3);
$categories = get_categories( array(
'taxonomy' => 'category',
'exclude' => $blog
) );
foreach( $categories as $category ) {
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
// echo $image;

How to Exclude Categories

$blog = array(3);
$categories = get_categories( array(
'taxonomy' => 'category',
'exclude' => $blog
) );

Where array is the ID of the desired category to exclude.

Categories

$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
    $term_img = wp_get_attachment_image( $thumb_id, 'full','',['class'=>''] );
    echo $term_img;

Have any questions or comments? Write them below!


Leave a Reply

Your email address will not be published. Required fields are marked *