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.
Related
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 );
Have custom post type posttype with multiple taxonomies like tax1, tax2 and post_tag which added like this:
function add_tags_categories() {
register_taxonomy_for_object_type('post_tag', 'posttype');
}
add_action('init', 'add_tags_categories');
And now searching for any solution to get most related posts by same tags count in posts.. most likely found solution like this:
$tags = wp_get_post_terms( $post -> ID, 'post_tag', ['fields' => 'ids'] );
$args = array(
'post__not_in' => array( $post -> ID ),
'posts_per_page' => -1,
'ignore_sticky_posts' => true,
'orderby' => 'count',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$my_query = new wp_query( $args );
But its not working.. I got $tags in array with ids,
query also seems working, just not finding any similar posts... but I have created and similar, and identical with tags also.. But result is still 0 posts..
Found solution in here
so I made like this:
$tags = wp_get_post_terms( $post -> ID, 'post_tag', ['fields' => 'ids'] );
$args = array(
'post_type' => 'posttype',
'post__not_in' => array( $post -> ID ),
'posts_per_page' => -1,
'ignore_sticky_posts' => true,
'orderby' => 'tag_count',
'order' => 'DESC',
'tag__in' => $tags,
);
add_filter( 'posts_clauses', 'wpse173949_posts_clauses', 10, 2 );
$query = new WP_Query( $args );
remove_filter( 'posts_clauses', 'wpse173949_posts_clauses', 10 );
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'),
)
)
);
I'm having a problem getting my query function. I need to run the loop, excluding a particular category.
I'm trying to use category__not_in, but is not working at all some.
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__not_in' => array( '44' ),
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
$query = new WP_Query( $args );
query_posts($query);
?>
I've already tried:
'category__not_in' => array( '44' ),
'category__not_in' => array( 44 ),
'category__not_in' => '44',
'category__not_in' => 44,
But nothing works =(
Try using tax_query instead :
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => get_query_var('paged'),
'tax_query' => array(
array(
'taxonomy' => '<YOUR TAXONOMY NAME>',
'field' => 'term_id',
'terms' => array( 44 ),
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
query_posts($query);
?>
Use 'cat' => '-44' in your $args array:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => '-44',
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
It's the way recommended in the WP Codex.
Thanks guys, it worked thanks to #rnevius
The problem was in my query, I was using WP_Query() and query_posts().
I used how reference the WP Codex: https://codex.wordpress.org/Class_Reference/WP_Query
Below is how my code was at the end:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__not_in' => array( 44 ),
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
$query = new WP_Query( $args );
?>
<?php
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
// code
<?php
}
} else {
// no posts found
}
wp_reset_postdata();
?>
To exclude a category in the search use this:
function search_filter($query)
{
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search)
{
$taxquery = array(
array(
'taxonomy' => 'category',
'field' => 'term_taxonomy_id',
'terms' => 244,
'operator' => 'NOT IN',
)
);
$query->set( 'tax_query', $taxquery );
}
}
}
add_action('pre_get_posts','search_filter');