Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a problem with query_posts orderby multiple ways. I have in Wordpress one category where I need display first posts with have a rank and after display all posts by title.

Below are working codes, that work well, but do not know how to connect these codes.

This code show me only posts with ranks, order by high ranks first

  query_posts(array('cat' => $category->term_id, 'posts_per_page' => -1, 'orderby' => 'meta_value', 'meta_key' => '_bunyad_review_overall', 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),));  

This code show me all posts orderby title

  query_posts(array('cat' => $category->term_id, 'posts_per_page' => -1, 'orderby' => title, 'order' => ASC, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),));  

I need connect these codes and in second code exclude all posts with ranks, because first code will show these posts.

  1. Show posts with ranks, orderby high rank first
  2. Show all posts, orderby title, exlucde posts with ranks

All this display in one page!

share|improve this question
    
You should never use query_posts. You should either use WP_Query or pre_get_posts –  Pieter Goosen Jun 9 at 5:46

1 Answer 1

First you need to use the WP_Query to get those posts. Then you can orderby using the following array.

// The Query
$args = array(
    'cat' => $category->term_id, 
    'posts_per_page' => -1, 
    'orderby' => 'meta_value',
    'meta_key' => '_bunyad_review_overall',
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
); 


$the_query = new WP_Query( $args );

// Create an array to store posts ID.
$posts_with_ranks = array();

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        // Your post content goes here

        // And also you can push each ID into this this array
        array_push( $posts_with_ranks , $post->ID );
    }
}

// reset post data
wp_reset_postdata();

Now we have our $posts_with_ranks with the posts with ranks. Let's suppose our array looks like this:

$posts_with_ranks = array(1,3,5,10);

Then your second WP_Query should look like this:

// The Query
$args = array(
    'cat' => $category->term_id,
    'posts_per_page' => -1, 
    'orderby' => title, 
    'order' => ASC, 
    'post__not_in' => $posts_with_ranks, // Exclude posts from the previous WP_Query
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
)

$the_query = new WP_Query( $args );

    // Create an array to store posts ID.
    $posts_with_ranks = array();

    // The Loop
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            // Your post content
            array_push( $posts_with_ranks , $post->ID );
        }
    }

    // reset post data
    wp_reset_postdata();

So basically you need to know this:

1) Use WP_Query to retrieve your posts with rank.

2) Store those posts into an array using array_push

3) Use a second WP_Query and exclude the posts with rank using post__not_in.

You can see the full wp_query arguments list.

https://gist.github.com/billerickson/3698476#file-gistfile1-php

This is pretty much a comprehensive reference of this.

http://codex.wordpress.org/Class_Reference/WP_Query

Hope this helps!

share|improve this answer
    
Hello Miguel, thank you for your reply. Please could you send me a full solution? Thank you for your time. –  user53902 Jun 8 at 22:53
    
I need connect these 2 query_posts to one ! Its possible? –  user53902 Jun 8 at 22:56
    
You have to use a WP_Query to retrieve your first posts and then create an array, push each $post->ID into this array. Then you have to create another WP_Query and retrieve the other posts excluding the ranked post from the array. I will modify this with a full solution. –  miguel Jun 8 at 23:00
    
Ok I updated the solution with some more details. –  miguel Jun 8 at 23:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.