• Resolved rsavard

    (@rsavard)


    Hi,

    I’m trying to show posts with query_posts(); using a meta_key and then order the posts shown by another meta_key. It’s not working because if I put the meta_key for displaying, query_posts() use it to order and if I put the meta_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

Viewing 12 replies - 1 through 12 (of 12 total)
  • 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 );
    Thread Starter rsavard

    (@rsavard)

    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.

    Thread Starter rsavard

    (@rsavard)

    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 help

    See 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";
    Thread Starter rsavard

    (@rsavard)

    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";
    Thread Starter rsavard

    (@rsavard)

    Yeah! Thanks a lot. I needed to add $wpdb to global $mam_global_join; and put $mam_global_join inside 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.

    Thread Starter rsavard

    (@rsavard)

    it’s not working. with $mam_global_join outside the function

    Well, use whatever works then.

    This was a great help to me just now! Many thanks!!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Show posts with query_posts using a meta_key & ordered using another meta_key’ is closed to new replies.