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:

image

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);
});

Error:

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.

Hi there,

has anything changed since then? I’ve reached a point where I also would like to query arrays of page IDs, as this is not an option for me in the query directly.

My temporary solution is to create a relationship field and add my pages via code and then feed that relationship into the query. However, this is not my preferred solution if the information is to be displayed on several pages.

A repeater would be practical in that case, but I would still prefer the query to give final touches via filters and other instead of edeting the code for each case.

Cheers

Edit: Just remembered this one, which would solve this better by directly calling a function return: Make all dynamic data available for Attributes and Tooltips + Queries?

My testing failed:

  1. Creating Query

grafik

  1. Register the Shortcode
function get_page_ids_to_display() {
    // Return the array of page IDs as string!
    return '2, 3';
}

// Register the shortcode
add_shortcode('page_ids', 'get_page_ids_to_display');

//[page_ids] Shortcode to paste
  1. Wordpress throws an error:
Fatal error: Uncaught TypeError: array_map():
Argument #2 ($array) must be of type array, string given in
\wp-includes\class-wp-query.php:2201 Stack trace: #0
\wp-includes\class-wp-query.php(2201): array_map('absint', '2, 3') #1
\wp-includes\class-wp-query.php(3824): WP_Query->get_posts() #2
\wp-includes\class-wp-query.php(3956): WP_Query->query(Array) #3
...

Any Ideas?

Cheers

Hi @T-low ,

Page IDs need to be passes as an array to WP_Query. However, the shortcode can only return a string and the Query Block doesn’t deserialize the string into an array before passing it to WP_Query. So using a shortcode like this to return a list of IDs doesn’t currently work. That’s why I am requesting to add support for a return function as it will allow us to write a function that returns the desired page IDs in an array.

1 Like

Hi @nadim,

thank you for the explanation, now I understand it too. :sweat_smile:

Cheers