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();
}
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 );
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.
Trying to implement a custom pagination in Wordpress and I cannot get the number of posts to dynamically create the pagination element.
$loop = new WP_Query(
array(
'post_type' => 'product',
'post_status ' => 'publish',
'orderby' => 'post_date',
'order' => 'date',
'posts_per_page' => $per_page,
'offset' => $start,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $suv_cates
)
)
)
);
$count = new WP_Query(
array(
'post_type' => 'product',
'post_status ' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $suv_cates
)
)
)
);
return $count->post_count;
The first query with the $loop returns the posts that I need. But when I return the $count or $count->post_count, it returns 0.
You need to reset the first query. So add this code after first loop and query
wp_reset_postdata();
wp_reset_query();
Then let me know the result.
Thanks
Posts per page is set to -1.
You could just count Loop?
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 have 2 custom post types created via theme, rt_book and rt_chapter. Each rt_book has several rt_chapter posts. When I use single-rt_book.php (which should show the contents of a specific rt_book post), how can I filter only the rt_chapter belongs to that specific rt_book being loaded?
My current WP_Query is as follow (which returns all rt_chapter):
$args = array(
'post_type' => 'rt_chapter', // TODO: Filter specific book.
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC'
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$posts[] = $query->post;
}
}
I relate these two custom post types by setting the Post Meta of rt_book with an array of rt_chapter post IDs, via function add_post_meta({post ID of rt_book}, 'chapter_orders', {post ID of rt_chapter}) and update_post_meta().
Tried child_of option of WP_Query, but does not affect the results.
Try Like this
$args = array(
'post_type' => 'rt_chapter',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'rt_book',
'value' => get_post_meta(get_the_ID(), 'chapter_orders', true),
'compare'=> 'IN'
),
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$posts[] = $query->post;
}
}
Try this. Hopefully this should work.
$tax_slug = get_query_var('taxonomy');
$args = array(
'post_type' => 'rt_chapter', // TODO: Filter specific book.
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'rt_book',
'field' => 'slug',
'terms' => $tax_slug
),
)
);