3

I have a custom post type, with a custom field associated with it. I've setup a WP_Query to return all the posts of that post type. What I want to do is to show the first one that meets a unique criteria for the custom field, and not any subsequent ones.

As an example let's say I have 5 posts, and for those 5 posts the custom field (let's call it 'size') is filled in as follows:

  • Post #1 - Field value: Small
  • Post #2 - Field value: Medium
  • Post #3 - Field value: Small
  • Post #4 - Field value: Large
  • Post #5 - Field value: Medium

What I would like my query to automatically detect when it reaches duplicate field vale, so the list of returned posts would be:

  • Post #1
  • Post #2
  • Post #4

Does anyone have any ideas?

  • Are you using the Advanced Custom Fields plugin to set and get your custom field values? Or have you manually built those in your theme/plugin? – Jared Cobb Jul 1 '14 at 15:10
  • 1
    Neither, I use CCTM plugin (wordpress.org/plugins/custom-content-type-manager) to build my custom types and fields. If you're unfamiliar with it I think it's better to say I am doing it manually, as that's probably a solution I can implement... – Kev Jul 1 '14 at 15:25
  • @Kev As plugin support is off topic, we are going to assume that. If the plugin you use does not support specific core stuff or does it it's own way, you will have to rework your setup anyway. – kaiser Jul 1 '14 at 15:29
  • Oh no this is not plugin specific. The query and all that is custom code I'm producing myself. The way I get the value from the custom field is manually built. – Kev Jul 1 '14 at 15:43
1

Based on the comments I think I'd implement this outside of the query itself, and use PHP to filter the results. I'll give you a second solution as well, but I think this first one is easier to understand (when you come back to it later on).

Since I don't know the specifics of your code, here's a pseudocode approach.

$unique_fields_array = array();

// you loop will contain all custom post types regardless of custom field values
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        // get the custom field for this specific post
        $meta_value = get_post_meta( $the_query->post->ID, 'size', true );

        // if we've never output a post with this custom field value yet, do it now, and then add it to the unique_fields_array
        if ( !in_array($meta_value, $unique_fields_array) ) {

            // OUTPUT YOUR POST CONTENT HERE

            $unique_fields_array[] = $meta_value;
        }
    }
}

// restore original postdata
wp_reset_postdata();

A second solution would be to bypass WP_Query all together (and I would not recommend it, but here it is anyway).

global $wpdb;
$values = $wpdb->get_results("SELECT DISTINCT meta_value FROM $wpdb->postmeta pm, $wpdb->post p WHERE meta_key  = 'size' and pm.post_id=p.ID and p.post_type='my_custom_postype' ", ARRAY_A);

You can then loop your $values in PHP and they'll only contain records where the size was unique. Of course, you can customize this SQL with a sort, additional filters, etc.

  • That's great, thanks for the advice. I used your pseudocode and applied it to my situation and got it working. – Kev Jul 2 '14 at 16:27
0

This is more in the realm of basic PHP, which is often overlooked with WP's loop "magic".

$values = array();

while( have_posts() ) : the_post();

    if ( in_array( $something, $values ) {
        continue; // we already saw this kind of something, skip the post
    } else {
        $values[] = $something; // seeing it for the first time, let's remember that
    }

    // usual loop stuff

endwhile; 
  • Ha, beat me to it by 1 minute :) – Jared Cobb Jul 1 '14 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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