0

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?

0

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

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.