Wordpress Sort Posts by custom Meta Value Date - php

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

Related

wordpress order by multiple

I have a query in wordpress that looks like this,
$args = array(
'post_type' => 'our-team',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => array(
'date_published' => 'ASC',
)
);
I am wanting to order my results by 2 attributes, firstly by date_published and then secondly my a meta value "weight". Weight is a numeric value (1 or 2).
When I change it the query to be,
$args = array(
'post_type' => 'our-team',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'weight',
'orderby' => array(
'date_published' => 'ASC',
'meta_value' => 'ASC'
)
);
When I run this query it only returns posts that have a weight of 1?
$args = array(
'post_type' => 'our-team',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'weight',
'orderby' => array( 'meta_value_num' => 'ASC', 'post_date' => 'ASC' )
);
Use date instead of date_published i think date_published is not the right key. and weight should be in int so better to use meta_value_num instead of meta_value try the below code.
$args = array(
'post_type' => 'our-team',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'weight',
'orderby' => array(
'date' => 'ASC'
'meta_value_num' => 'ASC'
)
);

WP_Query filtering

So I've been looking around here and other sites for a solution. I found lots of really helpfull posts but for some reason I just cant get this to work.
What I have:
WP posts with custom fields.
One is "rating" which is given a number between 1-5
The other is "flash" with either a 1 or a 0.
What I want to do:
Show all posts with a 1 on flash, in ORDER descending by the "rating"...
I currently have:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'meta_key' => 'flash',
'meta_value' => '1',
)
);
$ultimos = new WP_Query( $args );
This does NOT filter the flash custom field.
however if I do:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'flash',
'meta_value' => '1',
);
$ultimos = new WP_Query( $args );
This DOES filter flash, but does not order them properly.
Any thoughts?
I believe you need to take a look at using the relationship feature of the WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'flash',
'value' => '1',
'compare' => 'LIKE',
),
);
$ultimos = new WP_Query( $args );

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

Show pages and posts with 'true' checkbox value and sort 'em

not much add to the subj... I know how to do the first part:
$args = array('post_type' => array('post', 'page'),
'category__not_in' => array(7),
'meta_key' => 'show_on_main',
'meta_value' => true);
And the second part of the task should be something like this:
$args = array ('post_type' => array('post', 'page'),
'category__not_in' => array(7),
'meta_key' => 'sorting_on_main',
'orderby' => meta_value_num,
'order' => ASC);
But how can I do it both at the same time? I tried to do it with array in 'meta_key' value, but it didn't work out
$args = array(
'post_type' => array('post', 'page'),
'category__not_in' => array(7),
'meta_query' => array(
array(
'key' => 'show_on_main',
'value' => '1',
'compare' => '=='
)
),
'meta_key' => 'sorting_on_main',
'orderby' => meta_value_num,
'order' => 'ASC'
);

Exclude posts from get_posts()

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

Categories