get posts order by custom field - php

here, i want the post order by custom field(created by plugin Advanced Custom Fields), let say the posts order by the orders(custom field), then the categories with greatest custom field value post will appear first, is it available?
here my code is,
$args = array('category' => $cat1->term_id ,
'meta_key' => 'orders',
'orderby' => 'meta_value_num',
'order' => 'DESC' ,'numberposts' => -1
);
$myposts = get_posts( $args );
$i = 1;
foreach ( $myposts as $post ) : setup_postdata( $post );
echo get_field('orders',$post->ID);
$i++;
endforeach;

Can you pass 'post_type' =>'post' in $args array. So it will be like
$args = array('category' => $cat1->term_id ,
'post_type' =>'post',
'meta_key' => 'orders',
'orderby' => 'meta_value_num',
'order' => 'DESC' ,'numberposts' => -1
);
I am assuming that you are passsing post type = post. You can change there it if it's different.

Related

How do I get posts orderby title with some addtional meta key values

I was searching for this but didn't find the solution. I am getting posts orderby title but some posts need to come first so I added a custom field which meta_key is display_postion and the values for this fields are numbers i.e. 1,2,3. So how do I achieve this, below is my current code.
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby'=> 'title',
'post_status' => 'publish',
'posts_per_page' => -1,
'paged' => $paged
;
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts()) : $wp_query->the_post();
Adding meta_value_num to an orderby array and referencing 'display_position' in the meta_key field should do the trick:
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby' => array(
'meta_value_num' => 'ASC',
'title' => 'ASC'
),
'post_status' => 'publish',
'posts_per_page' => - 1,
'paged' => $paged,
'meta_key' => 'display_position'
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()):
while ($wp_query->have_posts()):
$wp_query->the_post();
See documentation for Order and Orderby parameters for WP_Query.

Wordpress custom query: 'orderby' => 'date' not working when using multiple post types

In a Wordpress page template, I set up a WP custom query which queries a custom post type named "recipe" AND the regular posts as below. This works, but 'orderby => 'date' in there doesn't work: The page first lists the regular posts ordered by date, then the recipes by date. But I need ALL of them together (i.e. mixed) ordered by date.
Here's the definition of my custom query:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...followed by the loop...]
I am grateful for any hints what I can do to achieve the desired ordering.
I found a solution myself:
It works when I add remove_all_filters('posts_orderby'); before the custom query is defined. Obviously this resets any other ordering and allows the 'orderby' => 'date' to function as expected. Complete code:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
remove_all_filters('posts_orderby'); // ADDED
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...]
Q1. what template file are you using?
The query below successfully merges the 2 post types together by date.
<?php
$args = array(
'post_type' => array('my_custom_post_type', 'post'),
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
// loop your content here
<?php include(locate_template('templates/content.php')); ?>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

Ordering products by price in custom wp_query loop

I currently have a very simple wp_query loop to loop through my WooCommerce products like so:
$args = array(
'posts_per_page' => -1,
'product_cat' => $cat,
'post_type' => 'product',
'orderby' => 'price',
'order' => 'DESC'
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
wc_get_template_part( 'content', 'product' );
}
This works as I want it to, except I can't get it to order the products by product price (ascending or descending) - what do I need to do to make this work?
Try this:
$args = array(
'posts_per_page' => -1,
'product_cat' => $cat,
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'asc'
);
Hope it will help you.

Displaying post with certain key value

I have a wordpress function that displays all posts of a custom meta.
PHP:
<?php
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
echo get_the_ID();
endwhile;
?>
This displays 4 posts per page. However, I only want to display those posts whose $key value is dogs.
Hope this helps.
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc',
'meta_value' => 'dogs'
);

Display only future events (cpt)

My current code shows 5 posts from the cpt 'events'. This is the code i'm using:
//get post type ==> events
global $post;
$args = array(
'post_type' => 'events',
'numberposts' => $data['home_events_count'],
'orderby' => 'meta_value',
'meta_key' => 'timestamp_earth_event_start_date',
'meta_query' => $meta_query
);
I want to filter out posts (events) that their 'timestamp_earth_event_start_date' are older than the current date.
I've tried to modify the code myself, but still past events are shown.
Any help would be most appreciated!
Try this one passing query string
$numposts=$data['home_events_count'];
query_posts('post_type=events
&post_status=publish&numberposts=$numposts
&posts_per_page=1
&meta_key=timestamp_earth_event_start_date
&orderby=meta_value_num
&order=DESC')
By passing array
$args = array(
'post_status' => 'publish',
'post_type' = 'events' ,
'meta_key' => 'timestamp_earth_event_start_date',
'order' => 'DESC',
'orderby' => 'meta_value_num'
);
$event_posts = new WP_Query( $args );
Other option you can add filter for the query like
add_filter( 'posts_orderby', 'my_posts_orderby_date', 10, 2 );
function my_posts_orderby_date( $orderby, $query ) {
global $wpdb;
return " CAST( $wpdb->postmeta.meta_value AS DATE ) " . $query->get( 'order' );
}

Categories