Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use posts_distinct filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 11, 2022
5 minutes read

posts_distinct filter

Filters the DISTINCT clause of the query.

Description

This is filter hook , its filter the DISTINCT clause of the query.
Returning “DISTINCTROW” from the filter has the same effect on the query as “DISTINCT”.Allows a plugin to add a DISTINCTROW clause to the query that returns the post array.
Its consists of two parameters, one is $distinct, second is $query.

Parameters

  • $distinct : (string) The DISTINCT clause of the query.
  • $query : (WP_Query) The WP_Query instance (passed by reference).

Live Example

To run the hook, copy the example below.

$array = apply_filters( 'posts_distinct', $array ); 
                         
if ( !empty( $array ) ) { 
                         
   // everything has led up to this point... 
                         
} 
// define the posts_distinct callback 
function filter_posts_distinct( $array ) { 
    // make filter magic happen here... 
    return $array; 
}; 
         
// add the filter 
add_filter( 'posts_distinct', 'filter_posts_distinct', 10, 1 ); 

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'posts_distinct', 'filter_posts_distinct', 10, 1 ); 

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.