پرش به محتویات

"Filter: bricks/helpers/get_posts_args"

فیلتر bricks/helpers/get_posts_args به شما امکان می‌دهد تا آرگومان‌های Query را که در هنگام بازیابی پست‌ها در builder از طریق AJAX استفاده می‌شوند را تغییر دهید.

این شامل سناریوهایی مانند:

  • جستجوی پست ها یا صفحات در کنترل نوع پیوند
  • جستجوی پست‌ها، صفحات یا انواع پست سفارشی در فهرست کشویی پر کردن محتوا

این قلاب به شما کنترل کامل روی آرگومان‌های WP_Query اساسی را قبل از اجرای Query می‌دهد.

یادداشت

این فیلتر بر چندین ویژگی سازنده در سطح جهانی تأثیر می‌گذارد. اصلاحات نادرست ممکن است رفتار مورد انتظار را در کنترل‌های پس از انتخاب شکست دهد.

پارامترها

  • $query_args (array): Array of arguments passed to get_posts().

نمونه استفاده

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.
    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'
    if ( $post_type_arg === 'any' ) {
        $query_args['post_type'] = $public_post_types;
    }

    // CASE 2: post_type is an array
    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;
} );