Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
        ...

    endwhile;endif;wp_reset_query();

    //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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.