Skip to content

Instantly share code, notes, and snippets.

@mdjwel
Last active September 11, 2021 06:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mdjwel/31307c9b910659c4113b924da4c20638 to your computer and use it in GitHub Desktop.
Save mdjwel/31307c9b910659c4113b924da4c20638 to your computer and use it in GitHub Desktop.
WordPress Custom Query Based Condition
<?php
$cats = get_the_terms(get_the_ID(), 'category');
$cat_ids = wp_list_pluck($cats,'term_id');
$related_post_count = !empty($opt['related_posts_count']) ? $opt['related_posts_count'] : 3;
$posts = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $cat_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => $related_post_count,
'ignore_sticky_posts' => 1,
'orderby' => 'rand',
'post__not_in'=>array($post->ID)
));
<?php
/**
* custom_post_query_with_pagination.php
* Show pagination in custom posts archive (loop) by custom post query $paged var
**/
global $wp_query;
global $paged;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts='.get_option('posts_per_page').'&post_type=t-shirts'.'&paged='.$paged);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment