Question

OK I need to query posts using the following criteria:

category_name=office, meta_key=featured_post, meta_value=Yes

and order these results using a second custom field which has a numerical value

meta_key=prop_order

I have the following query which pulls the correct posts, but doesn't order them by the second custom field.

<?php $recent = new WP_Query("category_name=office&meta_key=featured_post&meta_value=Yes&posts_per_page=3&orderby=date&order=ASC"); while($recent->have_posts()) : $recent->the_post(); ?>

However since I'm already using a custom field in the query I can't use orderby=meta_value.

I found the following SQL query on the forums but can't get it working.

    <?php
 global $wpdb;
 global $post;
 $querystr = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta AS proporder ON(
$wpdb->posts.ID = proporder.post_id
AND proporder.meta_key = 'prop_order'
)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = 3
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->wpostmeta.meta_key = 'featured_post' 
AND $wpdb->wpostmeta.meta_value = 'Yes' 
ORDER BY proporder.meta_value ASC
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

?>

Any help would be greatly appreciated!

Thanks

Dave

Was it helpful?

Solution 2

Thanks for your input guys. Didnt realise i'd never posted back on this! Thanks to Ethan and a few other foums heres the working code:

<?php
 global $wpdb;
 global $post;
 $querystr = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta AS proporder ON(
$wpdb->posts.ID = proporder.post_id
AND proporder.meta_key = 'prop_order'
)
LEFT JOIN $wpdb->postmeta AS propfeatured ON(
$wpdb->posts.ID = propfeatured.post_id
AND propfeatured.meta_key = 'featured_post'
)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = 4
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND propfeatured.meta_value='Yes'
ORDER BY proporder.meta_value ASC
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);
 //print_r($querystr);
?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>

<!-- some post stuff here -->

<?php endforeach; ?>

where 'prop_order' and 'featured_post' are the custom fields and posts are returned that match featured_post='Yes' and then they are ordered by 'prop_order' in ASC order.

hope this helps!

OTHER TIPS

I ran your query on my local workstation. It looks like there is a typo:

You should have $wpdb->postmeta.meta_value not $wpdb->wpostmeta.meta_value (used twice).

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top