[wp-hackers] wp-hackers Digest, Vol 54, Issue 27

Gwkg Archive gwkgarchive at gmail.com
Fri Jul 24 15:40:21 UTC 2009


Thanks scribd...

I couldn't find a(n easy) way to hook IFNULL into the query and that JOIN
statement I posted wasn't what was returning the duplicated results, it was
passing the query vars through an array that did it.  Problem is that I
tried so many different things yesterday I couldn't even duplicate it.

On the brightside, here is a solution

 function join_meta_value($q) {
  global $wpdb;
  $q .=  "LEFT JOIN (
                     SELECT *
                     FROM $wpdb->postmeta
                     WHERE meta_key =  'verification_value' ) AS metasort
            ON $wpdb->posts.ID = metasort.post_id";
  return ($q);
  }

function sort_by_value() {
  $orderby = "metasort.meta_value DESC";
  return $orderby;
  }

 add_filter('posts_join','join_meta_value');
 add_filter('posts_orderby','sort_by_value');

Thanks again for you responses.  Even though I didn't use them, I definitely
learned something from them.


> Message: 7
> Date: Thu, 23 Jul 2009 17:05:46 -0400
> From: Gwkg Archive <gwkgarchive at gmail.com>
> Subject: [wp-hackers] Sorting Posts By Custom Meta Value
> To: wp-hackers at lists.automattic.com
> Message-ID:
>        <270547670907231405m33cd9d02h67147674bd5af00f at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Ok I'm doing pretty good digging through the Wordpress Code and adding some
> functionality to a theme.  Heres what I did so far
>
> 1. Made one of my categories a Directory with its own template
> (category-9.php)
>
> 2. Added a drop-down box for posts in the Admin that lets the admin set a
> meta_key verification_level with a value of 0, 1, 2 or 3
>
> I want to sort on that value and I'm having a hard time figuring it out.
>
> A couple things <em>almost</em> worked like adding a post_join filter
>
> " JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id";
>
> but that was returning duplicated posts
>
> The closest (and easiest) mod was adding this line in category-9.php
>
> $posts = query_posts(
> "meta_key=verification_value&orderby=meta_value&order=desc" );
>
> but the problem is a lot of posts don't have the verification_value set and
> aren't being shown from that
>
> What's the best way to grab the posts without a value set in that meta_key
> and add them to the end of the post array without actually setting that
> value in the database?
>
>
> ------------------------------
>
> Message: 8
> Date: Fri, 24 Jul 2009 00:30:20 +0300
> From: scribu <scribu at gmail.com>
> Subject: Re: [wp-hackers] Sorting Posts By Custom Meta Value
> To: wp-hackers at lists.automattic.com
> Message-ID:
>        <349fe48b0907231430l2d90b708jf2fa6a7194bc7166 at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> >
> > " JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id";
> >
> > but that was returning duplicated posts
>
>
> You can add a GROUP BY wp_posts.ID to remove the duplicates.
>
>
> --
> http://scribu.net
>
>
> ------------------------------
>
> Message: 9
> Date: Fri, 24 Jul 2009 00:35:00 +0300
> From: scribu <scribu at gmail.com>
> Subject: Re: [wp-hackers] Sorting Posts By Custom Meta Value
> To: wp-hackers at lists.automattic.com
> Message-ID:
>        <349fe48b0907231435v356f7729vcf62808c3db93395 at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> >
> > What's the best way to grab the posts without a value set in that
> meta_key
> > and add them to the end of the post array without actually setting that
> > value in the database?
> >
>
> You could make the select look like this:
>
> SELECT ..., IFNULL(meta_value, 0) ...
>
> This will replace null values with 0.
>
>
> --
> http://scribu.net
>
>
> ------------------------------
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
>
> End of wp-hackers Digest, Vol 54, Issue 27
> ******************************************
>


More information about the wp-hackers mailing list