最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - How do I use `posts_distinct` correctly?

programmeradmin0浏览0评论

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>'; 
?>

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>'; 
?>
Share Improve this question edited May 31, 2019 at 2:15 breadwild asked May 30, 2019 at 18:38 breadwildbreadwild 3916 silver badges22 bronze badges 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. – Radley Sustaire Commented May 31, 2019 at 3:09
Add a comment  | 

1 Answer 1

Reset to default 2

You can see a practical example here: https://wordpress.stackexchange/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(); ?>
发布评论

评论列表(0)

  1. 暂无评论