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',
));
Related
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,
);
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 );
I want to order posts first by date and second by views
New update :
$trending_songs = new WP_Query(array(
'post_type' => 'songs',
'showposts' => $songs_count,
'cat' => 'songs_cat-689',
'orderby' => 'meta_value_num',
'meta_query' => array(
array(
'orderby' => 'date',
'order' => 'DESC',
),array(
'key' => 'views',
'order' => 'DESC',
)
),
));
the date is not working in the post (they just order by views)
You can use something like below code (this is just for sample you need to change according to your requirement)
<?php
$args = array(
'post_type' => 'words',
'meta_query' => array(
array(
'key' => 'word_count',
'orderby' => 'date',
'order' => DESC
,
),
array(
'key' => 'title',
'orderby' => 'date',
'order' => DESC
),
array(
'key' => 'word_type',
'orderby' => 'date',
'order' => DESC
),
),
);
$query = new WP_Query( $args );
?>
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.
I have a post with three meta-fields.
add_post_meta($my_post, 'times', $times);
And i would like to query this category and sort the posts by the meta field value of one of them.
The args i use right now are:
$args=array(
'post_type' => 'post',
'category_name' => 'players',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'times',
'meta_query' => array(
array(
'key' => 'times',
'value' => 0,
'compare' => '>=',
),
'posts_per_page'=> '8'
)
);
Where times is the name of the metafield.The above code isn't returning anything.
You have 'posts_per_page'=> '8' inside your meta_query argument.
Change your code into the following:
$args=array(
'post_type' => 'post',
'category_name' => 'players',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'times',
'meta_query' => array(
array(
'key' => 'times',
'value' => 0,
'compare' => '>=',
)
),
'posts_per_page'=> '8'
);