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.
Related
I am trying to call a custom field's metadata, and want to use it as a flag field for a Custom Post Type's Loop for a page. The field is 'tt_freemium'. The code I have below pulls everything and ignores the flag field. Uuuugh. What am I doing wrong ?
<?php $args = array( 'post_type' => 'membercontent', 'tt_freemium' => 'true', 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>
The answer in case anyone wants to know is to add a meta_query and an array to fill the meta query. It works now. Sorry to bother anyone that read this. ;-) Have a nice day y'all.
<?php $args = array(
'post_type' => 'membercontent',
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => 'true',
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>
I'm trying to order a wp_query that includes a meta_query...
After some different approaches, it seems that the query can't be ordered since I include the meta_query, and orderby => ID is used by default (I think) and it can't be overwritten. Any ideas on how to accomplish this?
Here's the PHP code I use just to display the posts by meta value:
$paged = get_query_var('paged') ? get_query_var('paged') : (get_query_var('page') ? get_query_var('page') : 1);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'subtitrare',
'value' => 'romana',
'compare' => 'LIKE'
)
)
);
$listing_query = null;
$listing_query = new WP_Query($args);
if ($listing_query->have_posts()) : get_template_part('loop-item');
endif;
wp_reset_postdata();
As you can see I also paginate the results, so i can use the same $args. I've tried to use a custom function to rearrange the results but it didn't worked for me.
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'orderby' => 'modified',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'subtitrare',
'value' => 'romana',
'compare' => 'LIKE'
)
)
);
This is the code you need but you already said that you tried this. This will return all the posts which have the custom field value set to LIKE 'romana'. What are the results you get from using this code?
Was looking for a while for a solution, but couldn't find one. So my question is, I have this code that sets posts order by default its DATE:
$args = array(
'post_type'=>'paibcresume',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(),
'tax_query' => array(),
'orderby' => 'date',
'meta_key' => '',
'order' => 'DESC'
);
I need some kind of a switch on the website, so user can pick how to order posts, for example it could be date to order by date, or modified to order by date of modification, or it could be a custom meta_key. How could I do that?
Check below url
http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
$query = new WP_Query( array ( 'post_type' => 'product', 'orderby' => 'meta_value_num', 'meta_key' => 'price' ) );
Looking for a way to loop through multiple custom post types and get the 4 most recent posts.
I have the below but doesn't quite work as if you 2 posts of the same post type, it'll show the most recent rather than showing most recent of ALL custom posts.
$args = array('post_type' => array('cs_trainee', 'cs_graduates', 'cs_pros', 'sd_trainee', 'sd_graduates'), 'posts_per_page' => 4, 'orderby' => 'menu_order', 'order' => 'ASC');
$careers = new WP_Query( $args );
Thanks
Just change the query_posts parameters a bit....
query_posts( array(
'post_type' => array( 'post', 'report', 'opinion', bookmark' ),
'cat' => 3,
'orderby' => 'date',
'order' => 'DESC',
'showposts' => 5 )
);
That should take care of it for you.
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'
); ?>