Query args of Posts Field seems to fail when arrays are passed
Support › MB Builder › Query args of Posts Field seems to fail when arrays are passedResolved
- This topic has 4 replies, 2 voices, and was last updated 4 years, 10 months ago by
Beda Schmid.
-
AuthorPosts
-
December 26, 2020 at 2:55 PM #23730
Beda Schmid
ParticipantIn the "posts" field it seems we can add Query Args, and it says to use WP Query Post Args.
One of those args ispost__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_inin 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?
December 26, 2020 at 5:55 PM #23735Long Nguyen
ModeratorHi 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' ), ),December 26, 2020 at 6:05 PM #23736Beda Schmid
ParticipantI 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
December 26, 2020 at 9:00 PM #23738Long Nguyen
ModeratorHi 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://support.metabox.io/topic/setting-custom-color-palettes-via-field-options/.
December 26, 2020 at 9:13 PM #23739Beda Schmid
ParticipantNice!
Didn’t see that, thanks Long!
-
AuthorPosts
- You must be logged in to reply to this topic.