Wordpress - fetch all data and sort

I am using Wordpress. I have the following query to retrieve data from a database and it works fine

$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 retrieve the rows where ID = '400' AND '403'. Therefore, when I echo, I receive only two information.

Now I'm trying to get all the data from the table, but when I show the information, I want to get a row where first the identifier is 400, then 403, and then the rest of the rows based on 'orderby' => 'title', AND 'order' => ' ASC '

Could you help with the request?

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); 
+4
source share
3 answers

Not sure if this will work in the query_post argument, but it adds valid SQL 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); 
+3
source

This will not be possible using the Wordpress query structure as you do. One possible solution is to execute a second query looking for results not in (400, 403), and just add this array of results to the end of your first array.

To do this, you should use get_posts() instead of query_posts() so that it does not change the main loop.

 $array = get_posts($args1); $array = array_merge($array, get_posts($args2); 
0
source

I suppose you could try 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 happens.

0
source

All Articles