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();
}
Related
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 );
Context: using Ajax with Wordpress.
functions.php:
$filterby = $_POST['filter'];
$orderby = $_POST['order'];
$args = array(
'post_type' => 'projects',
'posts_per_page' => -1,
// 'orderby' => $orderby,
'orderby' => array( 'title' => 'ASC', 'date' => 'DESC' ),
// 'order' => 'ASC',
'category_name' => $filterby,
);
$filterby regards post categories.
$orderby can be 'date' or 'title', depending on what the users choose.
If it is 'date', I want ``order' => 'DESC'. [displays latest posts first]
If it is 'title', I want ``order' => 'ASC'. [displays alphabetically, from a to z]
Thanks for taking the time.
simply use if statement for check
$filterby = $_POST['filter'];
$orderby = $_POST['order'];
$args = [
'post_type' => 'projects',
'posts_per_page' => -1,
'category_name' => $filterby
];
if($orderby == 'date'){
$args['orderby'] = 'date';
$args['order'] = 'DESC';
}elseif($orderby == 'title'){
$args['orderby'] = 'title';
$args['order'] = 'ASC';
}else{
unset($args['order']); unset($args['orderby']);
}
$query = new WP_Query($args);
$date_args = array(
'post_type' => 'projects',
'posts_per_page' => -1,
'orderby' => $orderby,
'order' => 'DESC',
'category_name' => $filterby,
);
$title_args = array(
'post_type' => 'projects',
'posts_per_page' => -1,
'orderby' => $orderby,
'order' => 'ASC',
'category_name' => $filterby,
);
if ( $_POST['order'] == date ) {
$the_query = new WP_Query( $date_args );
} elseif ( $_POST['order'] == title ) {
$the_query = new WP_Query( $title_args );
}
The following is returning no results. I want to filter by category and multiple tags. Can you see what i am doing wrong?
$tags = array( "blah-blah", "sausage" );
$posts = get_posts( array(
'posts_per_page' => 3,
'offset' => 0,
'category' => $categoryID,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag' => implode( ",", $tags )
) );
EDIT
This seems to work!:
$posts = get_posts( array(
'posts_per_page' => 3,
'offset' => 0,
'category' => $categoryID,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag_slug__in' => $tags
) );
Try something like this :
$posts = get_posts( array(
'posts_per_page' => 3,
'offset' => 0,
'category__and' => $categoryID,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag__in' => $tags
) );
Check if it works.
See below example to get posts by tags & category
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1); //get all posts
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;
I have a wordpress loop with an array of arguments to show only specific posts (any posts with a deposit_amount value of 0).
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0
);
$loop = new WP_Query( $args );
?>
I would like to create a similar array but showing posts with a deposit_amount meta_value of greater than 0
i have tried to use the php greater than operator but breaks the code.
'meta_value' => >0
Can anyone point me in the right direction with this problem?
just found 'meta_compare' => '>'
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0,
'meta_compare' => '>'
);
$loop = new WP_Query( $args );
?>
Use a Meta Query:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_query' => array(
array(
'key' => 'deposit_amount',
'value' => 0,
'compare' => '>'
)
)
);
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
);