Exclude duplicated posts from wordpress custom queries - wordpress

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;
(...)

Related

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()
)
);

Wordpress get Custom post title

I'm new to WordPress programming
I created a WordPress postal warehouse called "wp_locations"
I need to show the title of this post on the page
I put the following code in the my theme index file
<?php $gallery_args = array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'wp_locations',
'post_status'=> 'publish',
'suppress_filters' => true
);
$posts_display_gallery = get_posts( $gallery_args );
foreach($posts_display_gallery as $rows){
$post_title = $rows->post_title;
} ?>
But did not display the title
please guide me
Please use echo in your code
echo $post_title = $rows->post_title;
Please try below code and check it again....
<?php
global $post;
$gallery_args = array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'wp_locations',
'post_status'=> 'publish',
'suppress_filters' => true
);
$posts_display_gallery = get_posts( $gallery_args );
foreach ( $posts_display_gallery as $post ) : setup_postdata( $post );
$post_title = $post->post_title; // Like this you will get post title.
echo $post_title; // For display
endforeach;
wp_reset_postdata();?>
Hope this will helpful for you.
<?php $gallery_args = array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'wp_locations',
'post_status'=> 'publish',
'suppress_filters' => true
);
$posts_display_gallery = get_posts( $gallery_args );
foreach($posts_display_gallery as $rows){
$post_title = $rows->post_title;
echo $post_title; // for display the title
} ?>
You may try with this piece of code, should work
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ){
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$product_id = $loop->post->ID;
$product_title = $loop->post->post_title;
endwhile;
}
Try get_the_title() in the foreach loop

wordpress custom post type loop by tag id

I'm trying to make a loop of a custom type of posts by tag ID.
This is the original code:
<?php $args = array(
'post_type' => 'kana_portfolio',
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'posts_per_page' => -1
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
Since I need to loop only the posts in a certain category (of custom posts type) having id 31 I added "'tag_id' => 31" after "'posts_per_page' => -1"
<?php $args = array(
'post_type' => 'kana_portfolio',
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'posts_per_page' => -1,
'tag_id' => 31
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
[EDIT]
I have another loop below in the page and I tried to insert 'cat'=>31 tax_query array
<?php $number_posts_to_display = get_field('number_of_posts_to_display');
$display_order = get_field('post_display_order');
$portfolio = array(
'post_type' => 'kana_portfolio',
'posts_per_page' => $number_posts_to_display,
'order' => $display_order,
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'tax_query' => array(
array(
'taxonomy' => 'kana_genre',
'field' => 'slug',
'terms' => $portfolio_term_array,
),
),
);
$portfolio_loop = new WP_Query($portfolio); ?>
But nothing is showed, how can I do?
if your category (we are talking real categories here, of the taxonomy type category...) is 31, you should use 'cat'=>31, tags are tags, they are not categories.. if this helped, let me know. if you were trying to filter on tags and it was not working, let me know as well and ill take a look in depth.
Have fun!
Instead of:
'tag_id' => 31
Try this:
'tag__in' => 31

How to display all posts in WordPress?

I want to display all post in my WordPress home page.
I have written following query for getting all post but I do not get all posts. It just displays 10 or 11 posts:
$args = array(
'post_type' => 'post',
'posts_per_page' => $number,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
$args['tax_query'] = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-video',
));
$query = new WP_Query($args);
So please let me know how can I get all posts.
Displaying all posts that has been published. You have to use post_per_page='-1' to retrive all the posts.
$args = array(
'post_type'=> 'post',
'orderby' => 'ID',
'post_status' => 'publish',
'order' => 'DESC',
'posts_per_page' => -1 // this will retrive all the post that is published
);
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : ?>
<?php while ( $result->have_posts() ) : $result->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
Hope so this will retrive all the posts as per your expectation.
try Multiple loops :
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- do stuff ... -->
<?php endwhile; ?>
<?php endif; ?>
if you need Advanceded query use this :
<?php query_posts( 'post_type=post&posts_per_page=10'); ?>
and you can use query_posts() get all posts
Set the variable posts_per_page => -1
As so:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
This will make Query get all posts in your table.
See more in the WP Query docs

Wordpress Custom Post Meta Query

I'm trying query a custom post type for each comment with the dynamic field "comment_ID." I'm using the code below. This currently shows the comment ID, which I don't want, but does not show 'paid' as I would like.
<?php
$commID = comment_ID();
$args = array( 'post_type' => 'paidbriefs', 'meta_key' => 'Comment_ID', 'meta_value' => 'echo $commID', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo 'paid';
endwhile; ?>
</p>
I'm obviously doing something wrong with echoing the $commID variable as this does not show anything. If I change this to just $commID it returns 'paid' for every comment that has a Comment_ID meta, regardless of whether it matches the actual comment ID. Does anyone know how to fix this?
I think what you will need to do is a Loop, within a loop,
first loop to generate the standard loop, this will have your post info, comments etc.
within that loop you need to declare your comment_ID;
then from there, you setup another internal loop,
using the comment_ID for your custom field,
<?php
$args = array( 'post_type' => 'paidbriefs', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$commID = comment_ID();
$innerargs = array( 'post_type' => 'paidbriefs',
'meta_key' => 'Comment_ID',
'meta_value' => $commID,
'posts_per_page' => 10 );
$innerloop = new WP_Query( $innerargs );
while ( $innerloop ->have_posts() ) : $innerloop ->the_post();
echo 'paid Comment';
endwhile;
endwhile;
?>
untested though.
hopefully this will help,
<?php
$args = array( 'post_type' => 'ait-dir-item',
'meta_query' => array(
array(
'key' => 'location',
'value' => 'annapolis'
),
array(
'key' => 'item_tags',
'value' => 'non-marine'
)
),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title('<h3 class="entry-title">', '</h3>');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;?>
you can try this one

Resources