Search code examples
phpwordpresstaxonomyposts

Query posts with specific Taxonomy first


I'm trying to query posts in wordpress, so far so good. However, I would like to show a post with a specific tag 'info' in front of all (even in front of sticky).

I thought I could just merge the queries in an array like this:

 $posts = array( 'post__in' => $sticky, 'order' => $order_posts,  'ignore_sticky_posts' => 1, 'paged' => $paged ); 
$infoposts = array('tag' => 'info', 'post__in' => $sticky);

            query_posts ( array_merge( $infoposts, $posts ));

However this will only post the $infoposts array posts. How do I get both and then the posts with 'info' tag first?

How to do this?

Thanks!


Solution

  • array_merge merges your arrays together, var_dump the result of array_merge( $infoposts, $posts ) to see what i mean.

    AFAIK You'll need to do 2 separate queries, one for a sticky post and one for the other posts.

    -- edit --

    $sticky = get_posts($infoposts);
    $rest = get_posts($posts);
    
    $all = array_merge($sticky, $rest);
    
    foreach ( $all as $post ) : setup_postdata( $post ); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endforeach; 
    wp_reset_postdata(); // this is important ?>
    
    </ul>
    

    tldr: use get_posts instead of query posts

    http://codex.wordpress.org/Function_Reference/query_posts http://codex.wordpress.org/Template_Tags/get_posts