WordPress: OrderBY meta_key_value including null value - php

I am trying to make a simple query in wordpress ordered by a meta_value_num of a custom field.
$args1 = array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
);
I want to include the posts that have the custom field empty or null too ordered first or last in the query. How can I achieve that?

A simple and quick solution:
<?php
setup_postdata( $GLOBALS['post'] =& $post );
$posts= get_posts(array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
));
if( $posts ):
foreach( $posts as $post ): ?>
<!-- HTML HERE -->
<?php endforeach;
wp_reset_postdata();
endif; ?>
?>
If you want to get the generated SQL just pass it to the WP_Meta_Query object:
$query_args = array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
);
$meta_query = new WP_Meta_Query();
$meta_query->parse_query_vars( $query_args );
$res = $meta_query->get_sql(
'task',
$wpdb->posts,
'ID',
null
);

Related

Order by stock by new WP_Query - Woocommerce - Wordpress

I am making a new WP_Query to search for a product.
This shows me the wp_post information but not the stock information.
How can I order the stock results on top and the non-stock ones last in the search.
This is my current code.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'posts_per_page'=> $limit
);
$list = new WP_Query( $args );
Try this code but it doesn't work
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => '_stock',
'posts_per_page'=> $limit
);
$list = new WP_Query( $args );

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.

How to pull through custom post type by custom taxonomy?

I've made a custom post type type called "Videos" and within that post type a custom category called "Crystal" while using this plugin(https://wordpress.org/plugins/video-thumbnails/ to generate the YouTube videos thumbnail into the post.
I'm trying to pull through all of the Crystal posts and only display the video thumbnail on the page with a permalink to the post.
Here is my code;
<div class="block" id="home-three">
<p>YouTube</p>
<?php
$args = array(
'post_type' => 'videos',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_parent' => 0,
'tax_query' => 'crystal',
);
$count = 1;
?>
<?php $video_query = new WP_Query( $args ); ?>
<?php while ( $video_query->have_posts() ) : $video_query->the_post(); ?>
<div>
<a href="<?php the_permalink(); ?>">
<?php if( ( $video_thumbnail = get_video_thumbnail() ) != null ) { echo "<img src='https://wordpress.org/plugins/video-thumbnails/" . $video_thumbnail . "' />"; } ?>
</a>
</div>
<?php wp_reset_query(); ?>
</div>
According to documentation, the tax_query parameters accepts an array.
So your WP_Query arguments should be:
<?php
$args = array(
'post_type' => 'videos',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_parent' => 0,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'crystal'
)
)
);
?>
You could also use the Category Parameters:
<?php
$args = array(
'post_type' => 'videos',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_parent' => 0,
'category_name' => 'crystal'
);
?>

How to simplify multiple WP_Query with different meta values

Their are 6 total queries I need to use, below is list of 2 of them. Is it possible to simplify it? As you can see the key is the different values of meta_value, they are 1-6.
<?php
$args1 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => 1
);
$the_query1 = new WP_Query($args1);
if ($the_query1->have_posts()) {
while ($the_query1->have_posts()) {
$the_query1->the_post();
//do stuff
}
}
wp_reset_postdata();
?>
<?php
$args2 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => 2
);
$the_query2 = new WP_Query($args2);
if ($the_query2->have_posts()) {
while ($the_query2->have_posts()) {
$the_query2->the_post();
//do stuff
}
}
wp_reset_postdata();
?>
If you just want to get all the posts with meta values 1-6, you can pass as array
$args1 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => array( 1,2,3,4,5,6 )
);
$the_query1 = new WP_Query($args1);
Edited as per comments:
You could add the values to an array and run a foreach loop
$meta_values = array( 1,2,3,4,5,6 );
foreach ( $meta_values as $meta_value ) {
$args = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => $meta_value
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
//...
}
}
wp_reset_postdata();
}

WHERE NOT Query in Wordpress

I am querying into database using the following code and it is working fine
<?php query_posts(
'post_type=gallery&posts_per_page='.$gnum.'&paged='.$paged.'&orderby=title&order=ASC'
); ?>
Could you please tell me how to add WHERE NOT with the above query.
(WHERE ID != '400' AND ID!='401')
In wordpress query post add argument post__not_in.
'post__not_in' => array(400, 401)
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'post__not_in' => array(400, 401),
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC'
);
So your query post will be like,
query_posts($args);
Try this...
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'post__not_in' => array(6,2,8)
);
query_posts( $args );
<?php query_posts(
'post_type=gallery&posts_per_page='.$gnum.'&paged='.$paged.'&orderby=title&ID='-400,-401'&order=ASC'
); ?>

Categories