0

I currently have a list of posts with the custom field "Name," with several posts having the same value for "Name."

I would like to retrieve a list of posts that all have unique values for "Name." For example, Post 1 and Post 2 can't both be "Michael."

Is there a way to do this by altering the SQL query directly, before it's executed? I am currently using the function below (limit 5 posts and ordered randomly) to alter the query.

function alter_the_query( $request ) {
        $dummy_query = new WP_Query();  // the query isn't run if we don't pass any query vars
        $dummy_query->parse_query( $request );

        if ($dummy_query->is_category()) {
        $request['posts_per_page'] = 5;
                $request['orderby'] = 'rand';
        }
        return $request;
}

I am trying to avoid using SQL directly for sake of compatibility with future versions of Wordpress, but if that's the only way then I'm still open to those suggestions.

1 Answer 1

0

If you have several posts with the same value, but you want only the most recent ones with any value, as long as values don't get repeated, you'r best bet might be running two loops on the same query:

/* Fetch all possible values - use this to order the 
results and define which get selected */
$valueids = new WP_Query(array(
posts_per_page => -1,
meta_key => 'key',
));

/* First loop, to build an array of ids with single values */
$values = array();
$ids = array();
while ($valueids->have_posts()) : $valueids->the_post();
    $val = get_post_custom_values('key');
        if(!in_array($val,$values) {
            $values[] = $val;
            $ids[] = $post-ID;
        }
endwhile;

$valueids->rewind_posts();
$valueids->set( 'posts_per_page', 10 ) /* loop only for the ones you want */

/* Second loop, leaving out duplicated entries */
while ($valueids->have_posts()) : $valueids->the_post();
    if(in_array($post->ID,$ids) {
            // do you thing here
        }
endwhile;

ps: this is untested, but it should work properly.

4
  • I'm going with the 2nd loop idea you suggested! One question though: setting "posts_per_page" retrieves all posts. Is there a performance issue from a database call like this (from a list of 2000 posts)? Would it be better to set it to 20-30, just to make sure that when the doubles are removed in the 1st loop, i will still have enough posts for the 2nd loop.
    – mrl
    Commented May 22, 2012 at 20:39
  • You can alter it if you like, depending on how many repeated values you have. Let's say you're displaying 10 posts and of the last 10, 5 have repeated values, then you need to query for a minimum of 15 to be able to fetch 10 unique-value posts, get it? Should be safer, and faster, querying for 50, or 100.
    – moraleida
    Commented May 22, 2012 at 20:44
  • Yep, just asking if there was a significant difference in performance when querying a lower number like 30 as opposed to the full list of posts. Could you elaborate on why it would not only be faster, but safer as well?
    – mrl
    Commented May 22, 2012 at 20:50
  • Oops... typo. I meant "safe, and faster". As in "you'll probably be able to fetch the desired number of posts". :)
    – moraleida
    Commented May 22, 2012 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.