Exclude posts from get_posts() - php

Good morning, I found many similar questions, but none of the answer fit to my problem. The point is very simple: I have a custom loop with get_posts(), and I want to exclude current post from being displayed.
The code is:
$args = array(
'posts_per_page' => 3,
'orderby' => 'meta_value',
'order' => 'ASC',
'post_type' => 'fasthomepress_pt',
'post__not_in' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'custom_richiesta',
'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
I tried with:
'post__not_in' => array(get_the_ID),
'post__not_in' => array($post->ID),
'exclude' => $post->ID,
'exclude' => get_the_ID,
and with many other combinations with or without array. Of curse, current post id is correctly echoed before this loop, and if I try echo($post->ID) and echo(get_the_ID()) I have the same, correct, result.
I really don't know what's happening,
thank you very much for help,
Marco

Try exclude.
$args = array(
'posts_per_page' => 3,
'orderby' => 'meta_value',
'order' => 'ASC',
'post_type' => 'fasthomepress_pt',
'exclude' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'custom_richiesta',
'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);

here is a function that does just that:
function get_lastest_post_of_category($cat){
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
$post_is = get_posts( $args );
return $post_is[0]->ID;
}
Usage: say my category id is 22 then:
$last_post_ID = get_lastest_post_of_category(22);
you can also pass an array of categories to this function.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 18,
'paged' => $paged,
'offset' => 0,
'post__not_in' => array($last_post_ID,),
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
// The Query
$the_query = new WP_Query( $args );

Related

Wordpress Sort Posts by custom Meta Value Date

This is my code to query the posts but it doesnt work. I have a date format like this: dd.mm.yyyy
i changed the format a couple of times but it still dont work.
$args = array(
'meta_query' => array(
array(
'key' => 'gestorben',
'value' => date('dd.mm.yyyy'),
'type' => 'DATE'
)
),
'post_type' => 'traueranzeigen',
'showposts' => 10,
'paged' => $paged,
'suppress_filters' => $suppress_filters,
'meta_key' => 'gestorben',
'orderby' => 'meta_value',
'order' => 'DSC'
);
I hope you can help me
thanks it works now
$args = array(
'post_type' => 'traueranzeigen',
'showposts' => 10,
'paged' => $paged,
'suppress_filters' => $suppress_filters,
'meta_key' => 'gestorben',
'meta_type' => 'DATETIME',
'orderby' => 'meta_value_datetime',
'order' => 'ASC'
);

filter and sort wordpress get_posts with advanced custom fields

I have two custom fields created with Advanced Custom Fields.
One is type of checkbox (isEvent) and another is type of date (closing_date).
If I want to get all the posts that are events I would do something like this
<?php $args = array(
'posts_per_page' => 7,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => '',
'order' => '',
'include' => '',
'exclude' => '',
'meta_key' => 'is_event',
'meta_value' => 'a:1:{i:0;s:4:"true";}',
'post_type' => 'events',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => 0
);
$my_posts_array = get_posts( $args );
And this is working.
But If I would like to sort by closing_date which is the custom field of type date, in the ACF documentation for sorting is suggested that i should do something like this:
<?php $args = array(
'posts_per_page' => 7,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'meta_value_num',
'order' => 'asc',
'include' => '',
'exclude' => '',
'meta_key' => 'closing_date',
'meta_value' => '',
'post_type' => 'events',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => 0
);
$my_posts_array = get_posts( $args );
This is not working. Update - I managed to get this thing to work. I did not change anything, after few tests it worked....
Can someone give answer on any of these questions (or all) ?
What is the way to have two custom fields in wordpress and you want
to sort by one and filter by another ?
Is this possible in one call of get_posts or is there any other
native wp technique?
Can get_posts have meta_key, meta_value and meta_query in same
argument list for get_posts?
I also want to add also that I am using the WPML translation plugin with 2 languages en and fr.
The way to write the query is
$my_posts_array = get_posts( array(
'post_type' => 'events',
'posts_per_page' => '7',
'offset' => 0,
'post_status' => 'publish',
'suppress_filters' => 0,
'meta_query' => array(
array(
'key' => 'is_event',
'value' => 'a:1:{i:0;s:4:"true";}',
'compare' => '='
)
),
'orderby' => 'meta_value_num',
'meta_key' => 'closing_date',
'order' => 'ASC',
)
);
2.Yes this is possible in one call try above code or if not woeking then try this.
$my_posts_array = get_posts( array(
'post_type' => 'events',
'posts_per_page' => '7',
'offset' => 0,
'post_status' => 'publish',
'suppress_filters' => 0,
'meta_query' => array(
array(
'key' => 'is_event',
'value' => 'a:1:{i:0;s:4:"true";}',
'compare' => '='
)
),
)
);
// The Loop
foreach ($my_posts_array as $key => $value) {
$allowed_posts[] = get_the_ID();
}
$sorted_array = get_posts( array(
'post_type' => 'events',
'posts_per_page' => '7',
'offset' => 0,
'post_status' => 'publish',
'suppress_filters' => 0 ,
'post__in' => $allowed_posts,
'orderby' => 'meta_value_num',
'meta_key' => 'closing_date',
'order' => 'ASC',
);
foreach ($sorted_array as $key => $value) {
//do the stuff here
}
OK,
I managed to answer to this questions by myself.
1. This is one way of doing it:
<?php $args = array(
'posts_per_page' => 7,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => 'closing_date',
'meta_value' => '',
'post_type' => 'events',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'meta_query' => array(
array('key' => 'is_event',
'value' => 'a:1:{i:0;s:4:"true";}'
)
),
'suppress_filters' => 0
);
$my_posts_array = get_posts( $args );
2. I assume you can accomplish this in similar fashion with WP_Query since get_posts i actually using this method.
3.Answer is Yes like in the above example.

show pinned item at the very top of posts

I have created a checkbox with the help of metabox for pinning news items, just can't keep last pinned item be at the very top of list, here is my code:
$today = date("Ymd");
$args = array(
'post_type' => 'news',
'posts_per_page' => -1,
'meta_key' => 'pinned_news_item',
'meta_value' => '1',
'order' => 'DESC',
'orderby' => $today,
);
Finally found a solution. Here is a code that worked for me:
$args_meta = array(
'post_type' => 'news',
'posts_per_page' => -1,
'meta_key' => 'pinned_news_item',
'orderby' => 'modified',
'order' => 'DESC',
'ignore_sticky_posts' => '1'
);
Please try by editing your query:
$args = array(
'post_type' => 'news',
'posts_per_page' => -1,
meta_query' => array(
array(
'key' => 'pinned_news_item',
'value' => '1',
)
),
'order' => 'DESC',
'orderby' => $today,
);
Reference: http://www.happiweb.net/2015/05/code-wordpress-thuong-dung.html

Wordpress Query Args - Only show post with meta_value greater than 0

I have a wordpress loop with an array of arguments to show only specific posts (any posts with a deposit_amount value of 0).
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0
);
$loop = new WP_Query( $args );
?>
I would like to create a similar array but showing posts with a deposit_amount meta_value of greater than 0
i have tried to use the php greater than operator but breaks the code.
'meta_value' => >0
Can anyone point me in the right direction with this problem?
just found 'meta_compare' => '>'
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0,
'meta_compare' => '>'
);
$loop = new WP_Query( $args );
?>
Use a Meta Query:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_query' => array(
array(
'key' => 'deposit_amount',
'value' => 0,
'compare' => '>'
)
)
);

Multiple Array keys in WordPress get_posts Function

small question. I need to specify multiple meta keys and values in wordpress get_post function. Can anyone tell me how to do that?
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Okay, never mind, found my solution in the documentation. If someone needs it,
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
)
)
);
$postslist = get_posts( $args );

Categories