How can I pass the results of a WP_Query call to a plugin? I've tried passing the data via a shortcode attribute, but I get the error "Object of class WP_Query could not be converted to string".


$args = array(
// ...

$the_query = WP_Query($args);

echo do_shortcode( [example_custom_plugin query_payload="' . $the_query . '"] );

What are the different (and best advised) ways for a plugin to retrieve variables (which may be objects or arrays of data) that the WP template has set?

In my case, my plugin is instantiated via the shortcode call, hence why I am approaching it from that perspective; I want the plugin to have the results of the WP_Query at instantiation.

  • Why are you using a shortcode like this? Unless you also need users to be able to use the shortcode in posts, then just use a PHP function. Aug 21, 2020 at 0:38
  • @JacobPeattie The original idea was to turn it into a plugin that could be added wherever by any of the site's non-technical users. It's quickly looking like I've picked a weird method to accomplish my current goal, though. Aug 21, 2020 at 3:28

2 Answers 2


I think for shortcodes, which are generally used in the WYSIWYG editor by non-technical users, it's best to keep the parameters simple:

[custom-shortcode color="red" size="medium"]

In other words, shortcodes are designed to provide functionality that an end-user can place somewhere in their content, with the option of supplying a few different customizations.

For obtaining something as a complicated as WP_Query results, I would use:

global $wp_query;

That gives you access to the current query being executed at the moment that your shortcode is run. You can do that in the function that your shortcode would call.

If you need to make an entirely custom query for different data, you can in fact just create a new WP_Query:

function custom_shortcode() {
  $params = [];
  $q = new WP_Query($params);
  // ...

So it's best for your plugin to go lookup or get the data versus expecting the data to be passed. That's not always the case, but with what you presented, that seems like the best route.

  • Thanks! As you commented on another question I just asked, you may be curious to know that the reason I want to pass the results of the filtered data set into my plugin is actually so that the plugin's JavaScript can see the filtered data. Otherwise, it doesn't know how to stay "in sync" with the query results that the WP template page is spitting out. Aug 21, 2020 at 0:16
  1. Use global variable
  2. Pass just $args and run the same query inside the plugin
  3. Serialize results and pass it like you wanted

If I was you, I would prefer 2nd way...

  • Hi! Welcome to Stack Exchange. Do you mind elaborating on why you feel the second option is best (for the benefit of future readers as well)? Thanks! Aug 20, 2020 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.