WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a WP_Query loop running in a function that's attached to the 'admin_enqueue_scripts' hook

add_action( 'admin_enqueue_scripts', 'rs_get_scripts'); 
function rs_get_scripts() {
    //enqueue js file

    $args = array( 'post_type' => 'events', 'posts_per_page' => -1);
    $query = new WP_Query($args);                      
    if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

        /* bug occurs no matter what code is in here */
        //get data about each post


    //localize the data I got from each events post type into js file


What is happening is that running this loop at the time of 'admin_enqueue_scripts' hook seems to screw up the admin. The bug is that when I try to create a new post, page or any other Custom Post Type, Wordpress always redicrects me to to create a post of the post type I am querying in the loop above. In this case it's "events". I tried changing the loop to query_posts and get_posts but with no luck. Any ideas?

share|improve this question

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 );
share|improve this answer
Thanks for the help diggy! I just found another question that answers this so it's a dupe :/ – Roman Epicnerd Sharf Jan 15 '13 at 15:45
Here's the question that answers this: wordpress.stackexchange.com/questions/21882/… – Roman Epicnerd Sharf Jan 15 '13 at 15:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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