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).
Related
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 );
Hi I have this code to display only one category but I want to display category 16 and category 40. So only when the users needs to select category 16 and cat 40 then this will be display on the page:
<?php
global $post;
$args = array( 'posts_per_page' => 1, 'offset'=> 0, 'category' => 16 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
setup_postdata( $post );
?>
<div class="categoriesStyle"><?php exclude_post_categories("40"); ?></div>
<div class="first"><?php the_title(); ?></div>
<div class="paddingSpace"></div>
<div class="contentText"><span style="color: #000"><?php echo intro_text(150); ?></span></div>
<hr class="style-two">
<?php endforeach;
wp_reset_postdata(); ?>
To include more than one category you can use cat parameter instead of category
$args = array( 'posts_per_page' => 1, 'offset'=> 0, 'cat' => '16, 40' );
Or cat__in, that accept an array of category ids
$args = array( 'posts_per_page' => 1, 'offset'=> 0, 'cat__in' => array(16, 40) );
Be carefull about offset parameter that overrides $paged parameter (that you don't actually use in the present code, but can help), WP_Query class reference at Pagination Parameters part, says:
offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination (Click here for a workaround). The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.
Hope its helps!
I got the solution: I did this: Where main article is the other category. Category 14 is one category and the other category is main article (I didnt put the ID, rather I put the category name and it works!
$args = array( 'posts_per_page' => 1, 'offset'=> 1, 'category' => 14, 'category_name' => 'Main article' );
I have four categories in my Custom Post Type of "projects" that are 'live', 'heal', 'work', and 'play'. I have a few 'projects' posts with no category that I would like to omit from the archive page. In my archive, my query is as follows:
<?php $args = array( 'post_type' => 'projects','post_status' => 'publish', 'posts_per_page' => $projects_number, 'paged'=> $paged, 'category_name' => 'work,play,live,heal'); ?>
<?php $wp_query = null; ?>
<?php $wp_query = new WP_Query( $args ); ?>
<?php if ( $wp_query->have_posts() ) : ?>
But my archive is not returning any projects. I also tried the 'cat' parameter with id's but no avail there either. What am I missing?
For anyone who comes across this...the 'category' argument needed to be in its own array
I'm working on a page where I want to show posts (custom post type) from a category which have the same categoryname as the title of the post.
I'm still learning, that's Why I hope you guys could help me out. :)
What I have:
CPT named 'shoes'
CPT categories: sneakers, boots, sportshoes
CPT named 'articles'
CPT categories: sneakers, boots, sportshoes
On the page ~/sneakers/ (from CPT 'shoes') I want to show articles from the category 'sneakers'.
This what I have till now:
global $post;
$cat_ID = array();
$categories = get_the_category();
foreach($categories as $category) {
array_push($cat_ID,$category->cat_ID);
}
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => '[THE_TITLE]',
'numberposts' => -1,
'category__in' => $cat_ID
);
$cat_posts = get_posts($args);
if ($cat_posts) {
foreach ($cat_posts as $cat_post) {
?>
<li><?php echo $cat_post->post_title; ?></li>
<?php
}
}
OK, based on the question and the following explanation:
We are in the single-shoes.php where the information for single shoe is shown.
We want to grab and display the information about articles custom post type, and this articles must be in specific category, which name is = post_title.
If this is the case you can do the following:
We want our posts to be from articles custom post type so we must put 'post_type' => 'articles'in the $args array.
We want returned post to be in category, which name = post_title, so we must put 'category_name' => get_the_title() into $args
The code is:
<?php
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'articles',
'posts_per_page' => -1,
'category_name' => get_the_title()
);
$articles = new WP_Query($args);
if ( $articles->have_posts() ) :
while( $articles->have_posts() ) : $articles->the_post(); ?>
<li><?php the_title();?></li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
P.S. I'm still a little confused from the category name and still not sure that I understood correctly how this value is taken, however, if you can grab this value, the query is provided above.
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