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

How to use posts_fields filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 11, 2022
5 minutes read
Share Via:

posts_fields filter

Filters the SELECT clause of the query.

apply_filters_ref_array( 'posts_fields', string $fields, WP_Query $query )

Description

This is filter hook , its the SELECT clause of the query.
Its consists of two parameters, one is $fields, second is $query.
posts_fields seem to control which database fields will be returned in query, it seems to default to all fields from posts table.

Parameters

  • $fields : (string) The SELECT 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_fields', $array ); 
                         
if ( !empty( $array ) ) { 
                         
   // everything has led up to this point... 
                         
} 
  

The following example is for adding a hook callback.

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

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'posts_fields', 'filter_posts_fields', 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.