Displaying posts by two category in WordPress? - wordpress

I want posts that belong to 2 categories to be shown under my current posts, and the 2 categories must be determined by the current post that been displayed, so I write this code but not working, please help, tnx.
<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$category_id2 = $categories[1]->cat_ID;
$query = new WP_Query( array('category__and' => ($category_id, $category_id2),
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'publish_date',
'order' => 'DESC',);
while($query->have_posts()) : $query->the_post();
?>

It throws a php error because you forgot a ) at the end.
This shoud work:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$category_id2 = $categories[1]->cat_ID;
$query = new WP_Query(
array(
'category__and' => array( $category_id, $category_id2 ),
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'publish_date',
'order' => 'DESC',
),
);
while($query->have_posts()) : $query->the_post();
Try to use wp_debug in such cases

Related

Filtered a query in WordPress

To display some items from a post type, I'm using this WordPress query:
$posts = get_posts(array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC'
));
But how can I filter the datas returned by this query depending the infos in the post type page ? For example, I have a input 'year' to get the year of the project.
Thanks.
You can use wp_query like Below
$args = array (
'post_type' => array( 'realisations' ),
'post_status' => array( 'publish' ),
'order' => 'ASC',
'orderby' => 'date',
'year' => 'yourinputyear' // 2021
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post();
echo get_the_title();
endwhile;
}else{
echo "Data not found";
}
Please try this way. Hope is useful.
Thanks
You can use Date Parameters check below code.
$posts = get_posts( array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC',
'date_query' => array(
array( 'year' => 'yourinputyear' )
)
) );

Get all posts with help of WP_Query

I need get all posts of site. I try do it in widget, that I made myself, but result is empty.
global $post;
$args = array(
'post_type' => 'post',
'post_status' => 'publish'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ($query->have_posts()) {
$query->the_post();
var_dump($post->ID);
}
}
And when I add in argument array parameter cat then return posts, but I need get all posts from all categories, not just from specified categories.
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 22
);
Make sure the global $post is in the same function/scope.
If it still not working, instead of var_dump($post->ID) try:
var_dump($query->post->ID)
And don't forget to call wp_reset_postdata() after the while loop.
Try this:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_content();
endwhile;
wp_reset_postdata();
endif;
set posts_per_page to -1, this will return all posts from db.
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
// do stuff
}
I understood problem, on site (where I working in current time) was installed polylang, and when I specify for function get_posts in arguments 'lang' => pll_current_language() it return all posts for current language.
$results = get_posts(
array(
'numberposts' => 9999,
'orderby' => 'rand',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish',
'lang' => pll_current_language()
)
);

WP_Query for Archive Page Date not work

I have edited my theme on archive.php for show only a specific post and exclude a post with specific meta_key:
$posts_to_exclude_args = array(
'meta_key' => 'vip_box',
);
$posts_to_exclude = new WP_Query( $posts_to_exclude_args );
$to_exclude = array();
while ( $posts_to_exclude->have_posts() ) : $posts_to_exclude->the_post();
$to_exclude[] = $post->ID;
endwhile;
wp_reset_postdata();
$lastupdated_args = array(
'post__not_in' => $to_exclude,
'author__in' => $author,
'category__in' => $terms,
'posts_per_page' => 12,
'has_archive' => true,
'paged' => $paged,
);
$lastupdated_loop = new WP_Query( $lastupdated_args );
query_posts( $lastupdated_args );
<?php if ( have_posts() ) : ?>
And its perfect but now if i open a date link/url mywebsite.com/2017/06 it show all post and not only post in this date, why?.
Please can you help me?
I have fixed with this code, but i think there is a best solution than this. =)
$year = get_query_var('year');
$monthnum = get_query_var('monthnum');
$day = get_query_var('day');
'date_query' => array(
array(
'year' => $year,
'monthnum' => $monthnum,
'day' => $day,
),
),
);

Wordpress wp_query error in marketpress

I am using the following function to get markerpress products in the category 'featured' and display the product images in a slideshow.
$args = array(
'category_name' => 'featured',
'post_type' => 'product'
);
$wp_query1 = new WP_Query( $args );
if (have_posts()) : while ( $wp_query1->have_posts() ) : $wp_query1->the_post()
Problem is that it does not fetch any data. I am forced to just use the below function which displays all the data.
$wp_query1 = new WP_Query('post_type=product');
What am I doing wrong and how can I set limits and also sort the data. Thanks.
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category_name' => 'featured',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish'
);
$posts_array = get_posts( $args ); ?>

Exclude duplicated posts from wordpress custom queries

I'm building a template for a homepage which shows 4 latest posts on top and some groups of posts divided by category around the page (I'm using get_posts to perform queries).
What I'd like to do is exclude from these category posts any post already present in the latest four news on top.
I guess I should get that four post IDs and use the 'post__not_in' parameter in the "category" queries, but I can't make it work.
Dou you have any hints?
This is the code:
// First query: I get last four posts
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
// In my dreams, this should be the array of post IDs
$ids->query_vars['page_id'];
// Second query: I get 5 posts for a given categoory and exclude posts with IDs from first query
$query = new WP_Query($args);
$args2 = array(
'numberposts' => 5,
'category' => 15,
'orderby' => 'post_date',
'order' => 'DESC',
'post__not_in' => $ids
);
$query2 = new WP_Query($args2);
$primaposizione = get_posts( $args2 );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
... do the stuff ...
endforeach;
wp_reset_postdata();
UPDATE
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post();
$excludeID = $post->ID;
endwhile;
$args = array(
'posts_per_page' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'category' => 15,
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => array($excludeID)
);
$primaposizione = get_posts( $args );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
$category = get_the_category();
$slug = $category[0]->category_nicename ;
$esteso = $category[0]->cat_name;
if(has_post_thumbnail()) { ?>
<span class="hidden-xs"><?php the_post_thumbnail('bones-thumb-300', array('class' => 'img-responsive')) ?></span>
<?php } ?>
<h3 class="ellipsis"><?php the_title(); ?></h3>
<?php the_excerpt();?>
<?php endforeach;
wp_reset_postdata();
?>
The way you're trying to select the ID's is not going to work. You have to call wp_query first. I think this should work:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post(); ?>
$excludeID[] = $post->ID; // forgot the brackets
// do stuff
endwhile;
(...)

Resources