Custom Fields
Re-Envisioned
Support Meta Box Builder Query args of Posts Field seems to fail when arrays are passed

  • Creator
    Topic
  • #23730
    Resolved Beda Schmid
    Participant

    In the “posts” field it seems we can add Query Args, and it says to use WP Query Post Args.
    One of those args is post__not_in, which usually takes an array of post IDs to exclude.

    However, it doesnt seem to work, the Posts Select field will not offer any option at all, if we exclude a post with post__not_in in the fields query args.

    Perhaps I misunderstand the usage of these query args… but to me it seems, whichever Query Arg that takes an array will fail, so for example page_id (set to a single ID) will work, but none of the array args seem to work.

    Given there are a lot of WP Query args in array, perhaps we need to use a specific syntax when entering the values to the Fields settings?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #23735
    Long Nguyen
    Moderator

    Hi Beda,

    Could you please share the code you are using that has the setting query_args? The syntax should be:

    array(
        'name'        => 'Select a post',
        'id'          => 'my_post',
        'type'        => 'post',
    
        // Post type.
        'post_type'   => 'post',
    
        // Field type.
        'field_type'  => 'select',
    
        // Placeholder, inherited from <code>select_advanced</code> field.
        'placeholder' => 'Select a post',
    
        // Query arguments. See https://codex.wordpress.org/Class_Reference/WP_Query
        'query_args'  => array(
            'post__not_in' => array( 1, 123, 456 ),
            'order' => 'ASC',
            'order_by' => 'ID'
        ),
    ),
    #23736
    Beda Schmid
    Participant

    I was using the Builder (Backend UI).

    Indeed I can see now why this is, after reading the code you sent, I see a difference.
    The builder produces a code where the field query args array is a string:

    
    'query_args'  => [
        'post__not_in' => 'array( 773 )',
    ],
    

    Instead, it should be

    
    'query_args'  => [
        'post__not_in' => array( 773 ),
    ],
    

    So when using the AIO Builder, it will not register arrays correctly, it always seems to assume string as input, and produce string in the arg, which might even produce issues when the arg expects a numeric value.

    Perhaps this can be fixed in the builder, it would save time to edit code/save/reedit code etc

    #23738
    Long Nguyen
    Moderator

    Hi Beda,

    You can use the Dot notation to define an array in the Builder. See my screenshot https://share.getcloudapp.com/mXu5Dy5y.

    Refer to this topic: https://metabox.io/support/topic/setting-custom-color-palettes-via-field-options/.

    #23739
    Beda Schmid
    Participant

    Nice!

    Didn’t see that, thanks Long!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.