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'
); ?>
Related
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.
So I've been looking around here and other sites for a solution. I found lots of really helpfull posts but for some reason I just cant get this to work.
What I have:
WP posts with custom fields.
One is "rating" which is given a number between 1-5
The other is "flash" with either a 1 or a 0.
What I want to do:
Show all posts with a 1 on flash, in ORDER descending by the "rating"...
I currently have:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'meta_key' => 'flash',
'meta_value' => '1',
)
);
$ultimos = new WP_Query( $args );
This does NOT filter the flash custom field.
however if I do:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'flash',
'meta_value' => '1',
);
$ultimos = new WP_Query( $args );
This DOES filter flash, but does not order them properly.
Any thoughts?
I believe you need to take a look at using the relationship feature of the WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'flash',
'value' => '1',
'compare' => 'LIKE',
),
);
$ultimos = new WP_Query( $args );
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
);
So, I tried all the "add_filter" things on the whole web to get this right but it doesn't work! It always shows me more than 1 post. What's wrong with my code? Latest WP-Version 4.1.1 and no plugins installed.
Here is the code:
<?php
$sticky = get_option('sticky_posts');
if ( !empty($sticky) ) {
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1
);
$slider_query = new WP_Query( $args );
while ( $slider_query->have_posts() ) {
$slider_query->the_post();
?>
<div>
<!-- here we go -->
</div>
<?php
}
}
?>
Okay, I got it. This parameter is missing. Wordpress ignores "posts_per_page" by default for sticky posts.
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'ignore_sticky_posts' => 1
);
Wordpress doesn't play nice with posts_per_page and wp_query.
To fix it you have to use get_query_var, like so:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
And then in your wp_query() you do:
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'paged' => $paged
);
I am using Wordpress. I have the following query to fetch data from database and it is working perfectly
$args1 = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'post__in' => array(400, 403),
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC'
);
query_posts($args1);
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//And then some other code to display data
Using 'post__in' => array(400, 403),in the above query I am fetching the rows where ID='400' AND '403' . So when I echo, I get to see only two information.
Now, what I am trying to achieve is to fetch all data from the table but when I display the information I want to get the row where ID is 400 at first then 403 and then rest of the rows based on 'orderby' => 'title', AND 'order' => 'ASC'
Could you please help with the query?
Thanks
Edit
$args2 = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'post__not_in' => array(400, 403),
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC'
);
query_posts($args2);
Not sure if this will work within the query_post argument, but it is supplementing valid SQL in to it. Try:
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'paged' => $paged,
'orderby' => 'case when ID in (400,403) then -1 else title end, title',
'order' => 'ASC'
);
query_posts($args);
That is not going to be possible using the Wordpress query structure like you are doing. One possible solution is to do a second query looking for results not in (400, 403) and simply add this array of results to the end of your first array.
In order to do this, you should probably use get_posts() instead of query_posts() so that it doesn't alter the main loop.
$array = get_posts($args1);
$array = array_merge($array, get_posts($args2);
I suppose you could try doing the following:
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'paged' => $paged,
'orderby' => 'ID = 400 DESC, ID = 403 DESC, title',
'order' => 'ASC'
);
query_posts($args);
...
Let me know how this goes.