1

I've scoured the interwebs but can't find a good working example or discussion of the filter posts_distinct I'm using Advanced Custom Fields but I don't think that could be the problem. Anyway, nothing I try is working.

  1. Is where I have my function call correct? Or does it go ahead of the WP_Query?
  2. Do I have to pass any arguments?
//functions.php
function search_distinct() { 
    return "DISTINCT"; 
}
add_filter('posts_distinct', 'search_distinct');

//archive.php
<?php
    $args = array(
       'post_type' => 'homily',
       'posts_per_page' => -1,
       'meta_key' => 'homilist',
       'orderby' => 'meta_value', 
       'order' => 'ASC'
    );
    $the_query = new WP_Query( $args );

    search_distinct(); //Correct?

    if ( $the_query->have_posts() ) {
       echo '<ul>';
           while ( $the_query->have_posts() ) {
              $the_query->the_post();
                    echo '<li>' .the_field('homilist') . '</li>';
                }
        echo '</ul>';
     }
     wp_reset_postdata(); ?>

Update: Ended up with a less WordPress-esque solution than I had hoped for, the kind suggestions notwithstanding:

<?php
    global $wpdb;
    $querystr = "SELECT DISTINCT meta_value 
                FROM $wpdb->postmeta 
                WHERE meta_key = 'homilist' 
                ORDER BY meta_value ASC";
    $homilists = $wpdb->get_results( $querystr, OBJECT);

    echo '<select id="homilist">';
    for ($i = 1; $i < count($homilists); $i++) {
        echo '<option value="'.$homilists[$i]->meta_value.'">'.$homilists[$i]->meta_value.'</option>';
    }
    echo '</select>'; 
?>
1
  • My only suggestion to your new answer is to consider storing the $homilists data using set_transient/get_transient. Otherwise any time that code loads it's making a fairly expensive query in your database. You can store that data for a set period of time. And even better, you can clear the transient whenever you update a post so that you don't have to wait for the transient to expire for new posts to appear. Commented May 31, 2019 at 3:09

1 Answer 1

2

You can see a practical example here: https://wordpress.stackexchange.com/a/142902/19105

Why do you need to use DISTINCT in this situation though? The WP_Query you provided shouldn't be giving duplicates unless there is a plugin causing trouble.

But I'll try to answer it anyway. I would suggest keeping the function in functions.php. But move the filter to archive.php around the WP_Query call, like so:

add_filter('posts_distinct', 'search_distinct');
$the_query = new WP_Query( $args );
remove_filter('posts_distinct', 'search_distinct');

If you do this, I also recommend you leave a comment by the search_distinct function. Just explain that the function is used in archive.php -- it will help you out in the future if you forget what that function is from.

Also, remove the line: search_distinct(); //Correct?

That is not correct, you typically do not call action/filter functions directly. That is what the "add_filter()" is for.

Complete code:

// functions.php
// Used as a filter for archive.php to eliminate duplicate posts
function search_distinct() { 
    return "DISTINCT"; 
}

//archive.php
<?php
    $args = array(
       'post_type' => 'homily',
       'posts_per_page' => -1,
       'meta_key' => 'homilist',
       'orderby' => 'meta_value', 
       'order' => 'ASC'
    );

    // Get posts, make them distinct. search_distinct is defined in functions.php
    add_filter('posts_distinct', 'search_distinct');
    $the_query = new WP_Query( $args );
    remove_filter('posts_distinct', 'search_distinct');

    if ( $the_query->have_posts() ) {
       echo '<ul>';
           while ( $the_query->have_posts() ) {
              $the_query->the_post();
                    echo '<li>' .the_field('homilist') . '</li>';
                }
        echo '</ul>';
     }
     wp_reset_postdata(); ?>
2
  • I tried your suggestion and it did not return distinct names (there are over 1000 homilies but only 12 homilists—I only want the 12. Using posts_distinct , in a myriad of variations still turned all 1000. My approach might seem a bit odd, but I'm coming from a regular programming background, but still trying to play nice with WordPress—quite a learning curve.
    – breadwild
    Commented May 31, 2019 at 2:22
  • @breadwild I guess it wasn't clear what you needed to be unique. The distinct filter you brought up that I implemented would make post IDs unique (which should be the case anyway, unless you have some other plugin using joins or other filters inappropriately). Glad you got it working! Commented May 31, 2019 at 3:06

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.