Show posts with query_posts using a meta_key & ordered using another meta_key
-
Hi,
I’m trying to show posts with
query_posts();using ameta_keyand then order the posts shown by anothermeta_key. It’s not working because if I put themeta_keyfor displaying,query_posts()use it to order and if I put themeta_key,query_posts()order correctly but all post that have a order(They all have one!)Look like this:
$args = array( 'meta_key' => 'myPosts_displayed', 'meta_key' => 'myPosts_order', 'orderby' => 'meta_value_num', 'order' => 'ASC' ); query_posts( $args );Thanks
-
You will need to use a filter to add the second meta_key. Give this a try:
function mam_posts_join ($join) { global $mam_global_join; if ($mam_global_join) $join .= " $mam_global_join"; return $join; } add_filter('posts_join','mam_posts_join'); $mam_global_join = "JOIN $wpdb->postmeta metax ON (metax.post_id = $wpdb->posts.ID AND metax.meta_key = 'myPosts_displayed')"; $args = array( 'meta_key' => 'myPosts_order', 'orderby' => 'meta_value_num', 'order' => 'ASC' ); query_posts( $args );It almost worked. The order work but all the posts show. It seems that the filter for the displayed posts is’t working.
Thanks for your help Any other ideas ?
That really should work. I could not test the code, so I could have made a typo. Check it carefully to be sure I didn’t misspell a meta_key name.
This is strange beacuse when I put…
$args = array( 'meta_key' => 'myPosts_displayed' ); query_posts( $args );..it’s work well. Except that this time I don’t have the order. So the meta_key is working.
I’ll continue searching and I’ll post soon as I find something.
Thanks for your helpSee if the filter is getting run. Put in a print_r of the $mam_global_join variable:
function mam_posts_join ($join) { global $mam_global_join; print_r($mam_global_join); if ($mam_global_join) $join .= " $mam_global_join";Nothing is printed. Do I need to put something in functions.php or it is ok before the loop?
It is OK just before the loop.
Looks like maybe $mam_global_join is not getting set. Try setting it inside the function:
function mam_posts_join ($join) { global $mam_global_join; $mam_global_join = "JOIN $wpdb->postmeta metax ON (metax.post_id = $wpdb->posts.ID AND metax.meta_key = 'myPosts_displayed')"; print_r($mam_global_join); if ($mam_global_join) $join .= " $mam_global_join";Yeah! Thanks a lot. I needed to add
$wpdbtoglobal $mam_global_join;and put$mam_global_joininside de function has you told me.function mam_posts_join ($join) { global $wpdb, $mam_global_join; $mam_global_join = "JOIN $wpdb->postmeta metax ON (metax.post_id = $wpdb->posts.ID AND metax.meta_key = 'myPosts_displayed')"; if ($mam_global_join) $join .= " $mam_global_join"; return $join; } add_filter('posts_join','mam_posts_join'); $args = array( 'meta_key' => 'myPosts_order', 'orderby' => 'meta_value_num', 'order' => 'ASC' ); query_posts( $args );Thanks again for your help!
That was just a test! The proper way is to have $mam_global_join outside the function. I suspect there may have been a typo in the line when it was outside the function earlier.
Try moving the working line back outside the function.
it’s not working. with
$mam_global_joinoutside the functionWell, use whatever works then.
This was a great help to me just now! Many thanks!!
The topic ‘Show posts with query_posts using a meta_key & ordered using another meta_key’ is closed to new replies.