Wordpress orderby meta_value_num and order DESC issue - wordpress

I am trying to order my events with event date but i am facing an issue with order
$args = array(
'post_type' => 'post',
'category_name' => 'events',
'post_status' => 'publish', // just for me
'meta_key' => 'event_start',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 20,
);
thats how my query looks like. Problem is when events are in same date it loads A for B, C D its ok but when i refresh few times if A and B has same date it some times loads B first and sometimes A Could you please help me to find whats wrong with the query.

Thing is that probably Wordpress applies default ordering to the events which have the same events_start value. Try adding more order conditions to it, for example, by alphabetic order or by publication date:
// Alphabetic
$args = array(
'post_type' => 'post',
'category_name' => 'events',
'post_status' => 'publish', // just for me
'meta_key' => 'event_start',
'orderby' => array(
'meta_value_num' => 'DESC',
'title' => 'ASC',
),
'posts_per_page' => 20,
);
// By publication date
$args = array(
'post_type' => 'post',
'category_name' => 'events',
'post_status' => 'publish', // just for me
'meta_key' => 'event_start',
'orderby' => array(
'meta_value_num' => 'DESC',
'date' => 'ASC',
),
'posts_per_page' => 20,
);

Related

Wordpress - Sort post of one category alphabetically

I'm at the end, I'm not even sure if I'm doing the right thing.
I added the current code to the function.php, I need to change the order for one specific category. So I mean to say the category "internal-communication" to display posts by date added or alphabetically, but only one, I set a different global setting for the others.
Can anyone help me? Thank you.
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => 'interni-sdeleni',
'orderby' => 'post_date',
'order' => 'ASC',
'include' => 'interni-sdeleni',
'exclude' => '',
'meta_key' => '',
'meta_value' => 'interni-sdeleni',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$posts_array = get_posts( $args );
Try with this code -
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => 'interni-sdeleni',
'orderby' => 'name',
'order' => 'ASC',
'include' => 'interni-sdeleni',
'exclude' => '',
'meta_key' => '',
'meta_value' => 'interni-sdeleni',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$posts_array = get_posts( $args );

Wordpress Query by custom date filed

Can someone show me how to do this, I have a query I'm using to display custom posts and using WP Facet for the query, by a custom field date, I got this far to order the posts by date in Ascending order, but the last thing I want to do is only show the posts that equal today's date or future dates;
I've tried a few things but no joy as of yet, thanks
<?php
return array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 15,
'orderby' => 'event_date',
'order' => 'ASC',
'sort_custom' => true,
'meta_query' => array(
array(
'key' => 'event_date',
),
)
if this helps anyone - i worked it out:
<?php
return array(
'post_type' => 'event',
'post_status' => 'publish',
'meta_key' => 'event_date',
'posts_per_page' => 15,
'orderby' => 'event_date',
'order' => 'ASC',
'sort_custom' => true,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_date',
'compare' => '>=',
'value' => date("Y-m-d"),
'type' => 'DATE'
),
)
);

Order custom wordpress loop

I have a WordPress custom loop which looks like this:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'meta_key' => 'publication_type',
'meta_value' => 'uma',
);
This shows 6 posts with publication_type equal to "uma". The publication_type is a field created with Advanced Custom Fields.
I've also created the field publication_year which contains 2019, 2018, 2017,...
How is it possible to order all my posts descending?
The following example doesn't do a thing:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'order_by' => 'publication_year',
'order' => 'DESC'
'meta_key' => 'publication_type',
'meta_value' => 'uma',
);
$args = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'publication_type',
'value' => 'uma',
),
),
'meta_key' => 'publication_year',
'orderby' => 'meta_value_num',
'order' => 'ASC',
));

Query for getting sorted data using post meta field

I need sorted events using postmeta field which have value of date in format of 18 May 2017.This is my code but i dont get solution for sorting.
$args = array
(
'posts_per_page' => -1,
'post_type' => 'movies',
'meta_key' => 'event_release',
'orderby' => 'meta_value',
'order' => 'ASC'
);
Try specifying the meta type:
$args = array
(
'posts_per_page' => -1,
'post_type' => 'movies',
'meta_key' => 'event_release',
'meta_type' => 'DATE',
'orderby' => 'meta_value_date',
'order' => 'ASC'
);

Wordpress Query on multiple post types

I am completely stumped on this. The code below allows me to query multiple post types. I break them down like this because of the use of categories. The weird thing is, I only get posts from the post_type = 'post'. The final query I use post__in to establish the posts that I want by ID. If I print out $post_ids, I get the exact IDs that I am looking for. But my final query doesn't give me those IDs. Thoughts??
$postArgs = array(
'post_type' => 'post',
'cat' => '16,17,18',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$videoArgs = array(
'post_type' => 'occ-videos',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$photoArgs = array(
'post_type' => 'occ-photography',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$docArgs = array(
'post_type' => 'wpfb_filepage',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$posts_query = get_posts($postArgs);
$docs_query = get_posts($docArgs);
$video_query = get_posts($videoArgs);
$photo_query = get_posts($photoArgs);
// start putting the contents in the new object
$all_posts = array_merge($posts_query, $docs_query, $video_query, $photo_query);
$post_ids = wp_list_pluck( $all_posts, 'ID' );//Just get IDs from post objects
print_r($post_ids);
$artArgs = array(
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => 'post__in',
'post__in' => $post_ids);
$artQuery = get_posts($artArgs);
My understanding is that Wordpress always defaults to the post_type of post. So it's only finding posts that have one of those IDs – and ignoring your custom post types.
Trying adding a line to to your $artArgs
$artArgs = array(
'post_type' => array('post','page','occ-videos','occ-photography'), //Add this line
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => 'post__in',
'post__in' => $post_ids
);
And add whatever post types you need Wordpress to query.

Resources