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 try to query 5 posts in cpt 'vacancy'. Query is very complex. For first I need posts that belongs to custom taxonomy with ID 18 and sorted by 'post_modified', then - all other posts in cpt 'vacancy' also sorted by 'post_modified'. And all of this limited to 5 posts.

For now I use:

$query_hot = $wpdb->get_results("(SELECT * FROM wp_posts
WHERE post_type = 'vacancy' AND post_status = 'publish'
AND ID IN (SELECT object_id FROM wp_term_relationships WHERE
wp_term_relationships.term_taxonomy_id = 18)
ORDER BY post_modified DESC)
UNION
(SELECT * FROM wp_posts
WHERE post_type = 'vacancy' AND post_status = 'publish'
AND ID NOT IN (SELECT object_id FROM wp_term_relationships WHERE
wp_term_relationships.term_taxonomy_id = 18)
ORDER BY post_modified DESC) LIMIT 5");

It queries right posts, but not sorts by 'post_modified'. What I doing wrong?

share|improve this question

1 Answer 1

The WP_Query function can be used on custom taxonomies and post types and is probably easier than querying the database directly. The following should do what your looking for:

<?php $query_hot = new WP_Query( array(
    'post_type' => 'vacancy',
    'field' => '18', // You can use the ID or slug here
    'orberyby' => 'modified',
    'posts_per_page' => 5
)); 
while($query_hot->have_posts()) : $query_hot->the_post(); ?>

     <!-- Whatever you want to show in your loop -->

<?php endwhile; wp_reset_postdata(); // reset the query ?>

See the Codex article on WP_Query for more.

share|improve this answer
    
Thanks for reply! I know how this works, but it not exactly what I need. Example: I have total 12 posts in cpt 'vacancy' (this include 2 posts in taxonomy ID 18). Then my query should be: 1. simple post in taxonomy 18 (modified a hour ago) 2. simple post in taxonomy 18 (modified a day ago) 3. simple post (modified a hour ago) 4. simple post (modified a two hour ago) 5. simple post (modified a day ago) –  Vitaliy Sep 25 '11 at 8:22
    
It should be orderby and not orberyby –  VarunAgw Aug 11 at 22:27

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.