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

How to use posts_clauses filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
September 22, 2022
5 minutes read

posts_clauses filter

Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, fields (SELECT), and LIMIT clauses.

apply_filters_ref_array( 'posts_clauses', string[] $clauses, WP_Query $query )

Description

This is a filter hook that filters all query clauses at once for convenience. It consists of two parameters, one is $required and another is the $query Object.

The posts_clauses filter runs before the query gets executed and is essentially the sum of all filters that run immediately before it. So it should be used if you don’t intend to let another plugin override it, or if you need to alter several different parts of the query at once. If you’re only modifying a particular clause, you should probably use one of these clause-specific filters:

  • posts_where_paged
  • posts_groupby
  • posts_join_paged
  • posts_orderby
  • posts_distinct
  • post_limits
  • posts_fields

Parameters

  • $clauses : (string[]) Associative array of the clauses for the query.
    • where (string) The WHERE clause of the query.
    • groupby (string) The GROUP BY clause of the query.
    • join (string) The JOIN clause of the query.
    • orderby (string) The ORDER BY clause of the query.
    • distinct (string) The DISTINCT clause of the query.
    • fields (string) The SELECT clause of the query.
    • limits (string) The LIMIT clause of the query.
  • $query : (WP_Query) The WP_Query instance (passed by reference).

Live Examples

Basic Usage

To run the hook, copy the example below.

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

Adding a Hook Callback

The following example is for adding a hook callback.

    // define the posts_clauses callback 
    function weplugins_filter_posts_clauses( $clauses, $query  ) { 
        // make filter magic happen here... 
        return $clauses; 
    }; 
             
    // add the filter 
    add_filter( 'posts_clauses', 'weplugins_filter_posts_clauses', 10, 1 ); 
    

Removing a Hook Callback

To remove a hook callback, use the example below.

    // remove the filter 
    remove_filter( 'posts_clauses', 'weplugins_filter_posts_clauses', 10, 1 );  
    

Access Premium WordPress Plugins

Contact Us

If you need any customization, 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.