Get Bricks

Filter: bricks/helpers/get_posts_args

The bricks/helpers/get_posts_args filter allows you to modify the query arguments used when Bricks retrieves posts in the builder via AJAX.

This includes scenarios such as:

  • Searching posts/pages in the link-type control
  • Searching post/page/CPT in the populate content dropdown

This hook gives you full control over the underlying WP_Query arguments before the query is executed.

This filter affects multiple builder features globally. Incorrect modifications may break expected behavior in post selection controls.

Example: Exclude Media (Attachments) from Builder Search

By default, WordPress may include attachments (media items) when querying with post_type = any.
This example ensures that media items are excluded unless explicitly requested.

add_filter( 'bricks/helpers/get_posts_args', function( $query_args ) {
  // If no post type is provided, treat it as "any" for safety.
  $post_type_arg = $query_args['post_type'] ?? 'any';

  // Detect whether the caller explicitly requested the attachment post type.
  $has_attachment = false;

  if ( is_array( $post_type_arg ) ) {
    $has_attachment = in_array( 'attachment', $post_type_arg, true );
  } else {
    $has_attachment = ( $post_type_arg === 'attachment' );
  }

  // If attachment was explicitly requested, do nothing.
  // We only want to exclude it when it was not intentionally included.
  if ( $has_attachment ) {
    return $query_args;
  }

  // Get all public post types once. We only use this when expanding "any".
  $public_post_types = array_values(
    array_diff(
      get_post_types( [ 'public' => true ], 'names' ),
      [ 'attachment' ]
    )
  );

  /**
  * CASE 1: post_type = 'any'
  *
  * WordPress "any" may include attachments, so replace it with an explicit
  * list of public post types excluding attachment.
  */
  if ( $post_type_arg === 'any' ) {
    $query_args['post_type'] = $public_post_types;
  }

  /**
  * CASE 2: post_type is an array
  *
  * Remove attachment if present unexpectedly, and also support the unusual
  * edge case where "any" appears inside the array by expanding it into
  * public post types excluding attachment.
  */
  elseif ( is_array( $post_type_arg ) ) {
    $normalized_post_types = array_diff( $post_type_arg, [ 'attachment' ] );

    // Defensive handling: if "any" exists in the array, replace it with
    // all public post types except attachment.
    if ( in_array( 'any', $normalized_post_types, true ) ) {
      $normalized_post_types = array_diff( $normalized_post_types, [ 'any' ] );
      $normalized_post_types = array_merge( $normalized_post_types, $public_post_types );
    }

    $query_args['post_type'] = array_values( array_unique( $normalized_post_types ) );
  }

  return $query_args;
}, 10 );