I want to get posts that match three categories.
For example: if I have three categories, named 1, 2, 3, I want to grab the posts that belong to 1 AND 2 AND 3, and only that posts.
I found a way to make it with two categores:
$args = array(
'category__and' => array(5739,50),
'posts_per_page' => 10,
'orderby' => 'date'
);
But not three.
Thanks in advance.
If you want to show posts from several categories
Then you can display it using following code :
$query = new WP_Query( array( 'cat' => '2,6,17,38' ) );
If you want to show posts from several categories with AND condition
Then you can do it using following code:
$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );
If you want to show posts from several categories with OR condition
Then you can do it using following code:
$query = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );
You can use WP_Query for getting posts from multiple categories like:
query_posts( array( 'category__and' => array(34,26,29), 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ) );
Here is the helping link:
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Related
I want to show a random selection of 4 new products in WooCommerce.
For that I'm using a first loop to get the 20 newest products.
Like this:
$args= array(
'post_type' => 'product',
'posts_per_page' => 20,
'orderby' => 'date',
);
Now I've a second loop to reduce the products to 4 in a random order:
$args_new = array(
'posts_per_page' => 4,
'orderby' => 'rand',
);
In the end I merge the two loops:
$final_args = array_merge( $args, $args_new );
But that doesn't work. Is there any other way to achieve it?
General knowledge
The post_type argument accept String or Array.
Argument
Description
post_type
(String/Array) – use post types. Retrieves posts by post types, default value is post. If tax_query is set for a query, the default value becomes any.
Source # https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
Merging queries
In crude terms we want to combine 2 posts queries, retrieve each posts ID, push them to a new array and open a new query.
Keep in mind that if you want your 4 posts to be random (as you stated in the comments) they might be some duplicates of the last 20 from the first query. Don't forget to offset the second query.
<?php
// First query
$args_1 = get_posts( array(
'post_type' => 'dogs',
'post_status' => 'publish',
'post_count' => 20,
) );
// Second query
$args_2 = get_posts( array(
'post_type' => 'dogs',
'post_status' => 'publish',
'post_count' => 4,
'offset' => 20,
'orderby' => RAND,
) );
// 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 );
};
// Third query
$query = new WP_Query( array(
'post_status' => 'publish',
'post_count' => 24,
'post_in' => array_unique( $identifiers ),
) );
var_dump( $query );
I need your help. I'm trying to display 3 random products but skipping the first 3 most recently added products. Most recent meaning not by query but by global date the product was created.
Heres the code i use to display random products.
$args = array(
'post_type' => 'product',
'orderby' => 'rand',
'posts_per_page' => 3,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
}
Adding 'offset' only skips the first 3 random. Is there a way to skip first 3 most recently added products?
First, get three last products and get their IDs using wp_get_recent_posts function and map IDs, then add post__not_in argument to WP_query with these three post IDs
$recent_posts = wp_get_recent_posts([
'post_type' => 'product',
'numberposts' => 3
]);
$last_three_posts = array_map(function($a) { return $a['ID']; }, $recent_posts);
$args = array(
'post_type' => 'product',
'orderby' => 'rand',
'posts_per_page' => 3,
'post__not_in' => $last_three_posts,
);
$loop = new WP_Query( $args );
I'm trying to show one post from several categories. My code just shows the first category post :\ any advice?
<?php
$args = array(
'cat' => 1,15,
'post_type' => 'post',
'posts_per_page' => '1',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ($query->the_post()):
the_title();
the_post_thumbnail(array(200, 200));
?>
<?php endwhile;
endif;?>
Please follow the code to understand how to show post items from selected category items by passing the category ID.
$args = array(
'post_type' => 'post', // post type
'posts_per_page' => -1, // number of post items
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 16, 244 ) // pass the ID of the post category, separated by a comma.
)
)
);
You defined 'posts_per_page' => '1' so you are getting exactly what you ask: 1 post. Either from category 1 or 15, whichever is the most recent post.
If you want 1 post from EACH category, I would just loop your code, with a different category each time (just 1).
Only thing is, that will be in order of the category IDs you give and not sorted on date on something else. Also, if you have a post in multiple categories, you might end up with the same post twice.
I use this function:
<?php
$args = array(
'posts_per_page' => 6,
'category' => 317,
'orderby' => 'most_recent'
);
$the_query = new WP_Query( $args );
?>
Problem is it displays 4 posts from category "317" and 2 from another category. Why? I would like to have posts only form 317 category. (Now in this category there is 4 posts).
I am trying to exclude a specific post_type or pages, but I am not sure if I am thinking correctly. The issue is that all my pages comes up in my while query which is supposed to be dedicated for (almost) all my posts.
Here is what I am dealing with:
$args = array(
'post_type' => 'any',
'posts_per_page' => '-1',
'post_taxonomy' => 'any',
'cat' => -14,
);
I was thinking about writing 'post_type' => 'any' into posts, but as I remember that didn't work with my custom posts.
Do anyone have a working solution?
Thanks
If you are talking about filtering post with WP_Query you should read something here https://codex.wordpress.org/Class_Reference/WP_Query
Basically you can use a lot of filters, for example:
$args = array(
'post_type' => 'post',
'cat' => -14,
'post__not_in' => array( 2, 5 )
);
$query = new WP_Query( $args );
This will find only posts (not pages), not in category with id 14 and not those with post ID 2 or 5.
Now if you could be more precise in your question I could give you the exact array you need to obtain it.