Include posts with a shortcode in Query Editor

Not sure if this is a bug or me doing something wrong.

I am querying a specific post type and want to only specific posts ID. For this I am using the shortcode option as you can see in the screenshot below:

And this is my shortcode implementation:

But I am getting an error when loading the front end:


Anyone has experience using shortcodes in the Query Editor?

Hi @nadim,

Perhaps you can check whether the following field actually returns an array in all cases:

$modules = get_field('repeater_course_modules');

Hi @StrangeTech ,

Yes, $modules looks like this:

    [0] => Array
            [repeater_course_module_id] => 8495

    [1] => Array
            [repeater_course_module_id] => 8494


Tried using a different shortcode that just returns a hardcoded post ID and I am getting the same error on the front end:

add_shortcode('am_module_id_hardcoded', function ($atts = "") {
	return array(8494);


Warning: Array to string conversion in /public_html/wp-includes/shortcodes.php on line 355

Fatal error: Uncaught TypeError: array_map(): Argument #2 ($array) must be of type array, string given /public_html/wp-includes/class-wp-query.php:2191

I was looking at the array_key_exists function, and I hadn’t even noticed your return value - please see:

Thanks, @StrangeTech .

If I’m reading this correctly, then shortcodes shouldn’t be used as a dynamic value for “Include Posts” in the Query Editor because the shortcode will always return a string and a string cannot be fed to the WP_Query’s “post__in” attribute (AFAIK).

A better option would be to use a function instead of a shortcode. But unfortunately this isn’t currently available in the Query Editor (I’d love for this to be available everywhere. I might submit a feature request).

I need to figure out a way to work around this… if you have any suggestions, I’d love to hear them.

Thanks again.

Quick update…

To achieve what I need, I changed the implementation of the “shortcode” dynamic value implementation in the Query Editor (dynamic_query API) to interpret the key as function name instead of shortcode name:

//$return = do_shortcode('[' . $attributes['field'] . ']');
$return = cc_echo($attributes['field']);

Of course, changing Cwicly code like this is far from ideal as any change will be overwritten by an upgrade. Also, this breaks using shortcodes generally in the Query Editor (which I am not using for anything else) But this solves my problem temporarily.

@Louis , I could write and submit a proper patch to add support for “Functions” for dynamic values in the Query Editor if that’s something that you’d accept. Otherwise, I will submit a feature request.