Move the query part to a separate function that returns the data you need:
function rs_get_scripts_data(){
$array = array();
// query + add stuff to array
return $array;
}
and use it with wp_localize_script like:
$array = rs_get_scripts_data();
$myscript_vars = array(
'array' => $array
);
wp_localize_script( 'myscript', 'myscript', $myscript_vars );
Related Posts:
- Creating a table in the admin-style?
- body_class hook for admin pages
- WP_Query leaking absurd amounts of memory
- Change admin language based on user (in single-site)
- What is the real intention for admin-post.php?
- Fatal error: Class ‘ms\WP_Query’ not found on plugin initialization [closed]
- Register custom query args parameter for WP_Query()
- How to add multiple custom URL variables?
- Admin config screen without menu
- Make Categories and Tags required in admin
- Getting wrong relationship value in $args in wp_Query?
- Check for featured image in WP_Query
- Why Does get_posts() Return an Empty Set?
- How to add HTML / Form to an Admin Bar Menu
- How to get data from WordPress $wpdb into React Gutenberg Blocks Frontend?
- get_posts / WP_Query Memory size of 134217728 bytes exhausted
- How to ensure “the_content” filter runs only for the main displayed content?
- add_filter and remove_filter added before and after wp_query
- How to get all images and their thumbnails from wp media library
- Error with Custom Admin Screen in iframe Thickbox
- why don’t I get error messages in admin when developing?
- WP_Error handles errors, but how can I show success with a message?
- How to create a dynamic page based on form data with a plugin?
- How to Display Custom Post Type’s Gallery (images ) in Through WP_Query
- prevent showing posts of an specific category in admin posts section
- JS / JQuery form validation in backend admin menus
- How to put placeholder text in the main post input area?
- Handling form request from plugin file
- Is it possible to modify the media library admin screen?
- What does $_registered_pages do?
- Get user id for delete and update selected user
- Cannot search post by taxonomy
- How to get orders with used coupon in WooCommerce
- WP_Query with tax_query, order by most ‘matches’
- Add tab to profile.php wordpress plugin development
- WordPress Ajax callback function from plugin – OOP
- How to print raw query from WP_Query class just like in CodeIgniter
- Admin auto-refresh is interfering with Firebug — how to prevent it?
- WP_Query returns no results
- Finding posts containing matching array elements in a meta field usign WP_Query
- Including a JS source with an admin page
- Create a custom display order in the main menu
- WordPress not working on localhost
- manage_posts_custom_column, showing serial number using increment in loop
- Error : “Cannot use object of type WP_Post as array in”
- Proper way to run wp_query from inside a plugin
- using new WP_Query in save_post function alters $post
- maintaing consistent layout wordpress dashboard
- Slow WP_query due to nested wp_query. Need Suggestions
- Add column and post filter for a custom post type field on the edit.php page
- Is there no admin ui guide for 4.x?
- ORDER BY wp_post custom column name in wp_query
- Correct check for any admin page with editor
- How to create database table, add data, update and delete using wpdb via plugins?
- Adding (blog-specific) links to “My Sites” admin page
- Change the look and feel of admin pages
- Admin Plugin POST to another file within the plugin
- Elementor custom Query with ACF fields to show matching woocommerce products custom fields
- WP_Query filters
- How to fetch only current hour posts?
- How to get Metabox custom field to show checked if value is updated using post meta query?
- Admin – Handle data before creating or updating a post, page or custom post
- WordPress pre_get_posts with combined results of two queries (OR)
- How can I replace content in the WP Admin area before an admin page is rendered?
- Search: how to extend the existing search to include a custom table
- Add a page in admin without adding menu item
- Including comments meta box on a plugin page
- how to get all the child category name in a specified category name?
- how to invoke wordpress API from other existing PHP system
- How to Move the Comments Bubble to the Right Side of the Toolbar
- Does admin_print_scripts-$hook_suffix work for nested paths to individual files?
- How I can use order by of the custom post title?
- Use ‘get’ form action within a WordPress plugin admin page
- How to query children by post name/slug
- 400 Bad Request, in wordpress theme development, wp_ajax
- Prevent WordPress automatically processing certain URL queries
- Store custom meta box data as serialized array
- how to load the comment template from a plugin
- Making Woocommerce optimized for more than 500k products
- Post Pagination does not working on WP-Query
- WP Query Relations / Compare
- pull data from wordpress database
- How can I query 3 posts from diferente categories (one of each category)
- Query only title/field/featured media of posts [closed]
- How to return a blank page
- Custom MySQL query to pull out Advanced Custom Fields?
- Adding admin for specific users
- How do I control the list of Pages an author can see?
- post_results filter confused by pagination in sidebar and main section
- What happens when you create/edit a menu
- Can’t get query string in ajax call
- Get custom wp_query search results to appear on search.php
- How do I query posts and have their related taxonomies returned in the results?
- modify buddpress adminbar only in admin pages
- Best Way to Inventory the Media Library of a 200+ Multisite Installation?
- $_GET vs get_query_var()
- What is the meta_query key name for the woo product average rating? [closed]
- get the queried_object of an url
- Return custom product in ajax call loop
- filter on get_posts efficiently