I have a custom taxonomy called type. Type has the following options:
Blog
Case study
Webinar
When on a post, I want to showcase posts which have the same type. I.e. if you're on a case study, I want it to display other case studies.
The maximum related posts I want to show is two, but if there is only one post that has that tag, then I only want it to display one.
Not sure where my query is falling apart:
global $post;
$blog_type = get_terms('type');
$args = array(
'post_type' => 'resources',
'category__in' => wp_get_post_categories($post->ID ),
'posts_per_page' => 2,
'post__not_in' => array($post->ID ),
'terms' => $blog_type,
);
$relatedPosts = new WP_Query( $args );
You need to use tax_query here:
$term_list = wp_get_post_terms( $post->ID, 'type', array( 'fields' => 'ids' ) );
$args = array(
'post_type' => 'resources',
'posts_per_page' => 2,
'post__not_in' => array( $post->ID ),
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'term_id',
'terms' => $term_list
)
)
);
$relatedPosts = new WP_Query( $args );
Related
I am having issues getting my posts to be shown by the tag-name in Wordpress. I have a Custom Post Type called Reviews with a (taxonomy?) category named Gametypes. Within Gametypes I have a tag named Newest.
I am trying to get posts to display which is tagged by the Newest tag. I have tried the following code which does not work, and I am not sure why:
$args = array( 'tax_query' => array( array( 'taxonomy' => 'Gametypes', 'field' => 'slug', 'terms' => 'newest' ) ) );
$postslist = get_posts( $args );
I've tried different iterations of it with and without capital starting letter but I cannot seem to get it to work. Anyone who could shed light upon what I am doing wrong? I am able to pull every post from Reviews (posts with and without tags) with this code:
$args = array( 'post_type' => 'Reviews', 'numberposts' => 6, 'order'=> 'DESC', 'orderby' => 'date' );
In case this information helps. I hope someone can guide me in the right direction!
I found the answer myself, in case anyone wonders!
$args = array(
'post_type' => 'Reviews',
'numberposts' => 6,
'tax_query' => array(
array(
'taxonomy' => 'gametypes',
'field' => 'slug',
'terms' => 'newest',
),
),
);
$query = new WP_Query( $args );
$postslist = get_posts( $args );
Use this one:
<?php
$query = new WP_Query(
array( "post_type" => "Reviews", // "your-post-type" !
"tag" => "Newest"
) );
while ($query->have_posts()) : $query->the_post(); ?>
<?php endwhile; ?>
I'm looking to use get_posts to return all posts within a specific term, plus some specific posts that may exist in another term. Ideally, I want to do this within one query. The issue is tax_query overrides any posts that aren't in that term. This is the code:
$args = array(
'numberposts' => -1,
'post_type' => 'books',
'post_status' => 'publish',
'include' => array(3181, 3180), //these are specific books that can be in any term
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'id',
'terms' => array(152) //plus I need to get all books within this term
);
);
);
$my_posts = get_posts( $args );
var_dump($my_posts);
This only returns the posts within the fiction genre. This does not return the books with ID's 3181 and 3180.
Your help is much appreciated.
post__in is used to retrieve specific posts ids.
Argument
Description
post__in
(array) – use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.
Source # https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
<?php
// First query, taxonomy query
$args_1 = get_posts( array(
'post_status' => 'publish',
'post_count' => -1,
'tax_query' => array(
array(
'taxonomy' => 'module',
'field' => 'slug',
'terms' => '010-01-01',
)
),
) );
// Second query, specific posts query
$args_2 = get_posts( array(
'post_status' => 'publish',
'post_count' => -1,
'post_in' => array( 89, 9156, 6874, 615, ),
) );
// Merge queries
$posts = array_merge( $args_1, $args_2 );
// Push posts IDs to new array
$identifiers = array();
foreach ( $posts as $post ) {
array_push( $identifiers, $post->ID );
};
// New query
$query = new WP_Query( array(
'post_type' => 'question',
'post_status' => 'publish',
'post_count' => -1,
'post_in' => array_unique( $identifiers ),
) );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
the_title( '<h1>', '</h1>' );
endwhile;
endif;
I have three posts, two which have the tag eGuide one which has the tag Article. When I click on an eGuide, I want the sidebar to display other eGuide's (up to 2) and nothing else. To do this, I have the following query:
global $post;
$args = array(
'post_type' => 'resources',
'category__in' => wp_get_post_categories($post->ID ),
'posts_per_page' => 3,
'post__not_in' => array($post->ID )
);
$relatedPosts = new WP_Query( $args );
But it's showing the article too? I've also tried:
'post__not_in' => array(get_the_ID() )
... still no luck.
global $post;
$term_list = wp_get_post_terms( $post->ID, 'your_taxonomy_here', array( "fields" => "ids", "parent" => 0 ) );
$args = array (
'post_type' => 'resources',
'posts_per_page' => 3,
'post__not_in' => array( $post->ID ),
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'your_taxonomy_here',
'field' => 'term_id',
'terms' => $term_list[0],
),
),
);
$relatedPosts = new WP_Query( $args );
You can try this code to get related posts.
I'm collecting statistics and there I want to show all pages, posts, taxonomy categories I have.
I could display post types and pages because actually they all have some post type but can't display taxonomy categories together with them:
<?php
$excluded_ids = array(1, 5);
$postArgs = array(
'post_type' => array('page', 'products'),
'order' => 'ASC',
'orderby' => 'title',
'post__not_in' => $excluded_ids,
'posts_per_page'=> -1,
/*'taxonomy' => 'product-category'*/
);
$postList = get_posts($postArgs);
?>
Is there a way to display everything (posts,categories,pages) via single query and not multiple? Any ideas?
The get_posts method doesn't accept array as the post_type value. You should use WP_Query to query the posts.
$args = array(
'post_type' => array( 'page', 'products' )
);
$query = new WP_Query( $args );
If i understanded you correctly
$argss = array(
'post_type' => array('page', 'products'),
'order' => 'ASC',
'orderby' => 'title',
'tax_query' => array(
array(
'taxonomy' => 'your taxonomies',
'field' => 'slug',
'terms' => 'your term',
),
),
);
$the_queryy = new WP_Query( $argss );
if ( $the_queryy->have_posts() ) {
while ( $the_queryy->have_posts() ) {
$the_queryy->the_post();
// echo stuff
}
wp_reset_postdata();
}
I am trying to display all custom post types of a specific taxonomy in WP.
Here is the code:
<?php
$args = array(
'post_type' => 'team',
'orderby' => 'rand',
'posts_per_page' => -1
);
$trit = new WP_Query($args);
while ($trit->have_posts()) : $trit->the_post();
$post_id = get_the_ID();//POST ID
$terms = get_the_terms($post->ID, 'tax_members' );
...
endwhile;
The script above displays all members of all taxonomies (tax_members). My goal is to display only the members of a specific category...for example: players.
How can I call the taxonomy players inside
$terms = get_the_terms($post->ID, 'tax_members' );
<?php
$queryArr = array(
'post_type' => 'team',
'orderby' => 'rand',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tax_members',
'field' => 'slug',
'terms' => 'players',
'operator' => 'IN'
),
),
);
$trit = get_posts( $queryArr );
?>
Try this
$args = array(
'post_type' => 'team',
'orderby' => 'rand',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tax_members',
'terms' => array('players'),
)
)
);