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
December 17, 2022
5 minutes read

posts_distinct filter

Filters the DISTINCT clause of the query.

Description

This is a filter hook that filters the DISTINCT clause of the query. Returning “DISTINCTROW” from the filter has the same effect on the query as “DISTINCT”. It allows a plugin to add a DISTINCTROW clause to the query that returns the post array.

It consists of two parameters: one is $distinct, and the second is $query.

Parameters

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

Live Examples

Basic Usage of posts_distinct Filter

To run the hook, copy the example below.

$array = apply_filters( 'weplugins_posts_distinct', $array ); 
                         
if ( !empty( $array ) ) { 
   // everything has led up to this point... 
} 

Adding a Custom Filter Callback

Define a callback function to customize the DISTINCT clause, then add the filter using the code below.

// define the weplugins_posts_distinct callback 
function weplugins_filter_posts_distinct( $array ) { 
    // make filter magic happen here... 
    return $array; 
}; 
         
// add the filter 
add_filter( 'weplugins_posts_distinct', 'weplugins_filter_posts_distinct', 10, 1 ); 

Removing a Hook Callback

If you need to remove a previously added filter callback, use the following code.

// remove the filter 
remove_filter( 'weplugins_posts_distinct', 'weplugins_filter_posts_distinct', 10, 1 ); 

Access Premium WordPress Plugins

Contact Us

If you need help with customization or have any queries, feel free to Contact Us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

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.