I am creating a JSON API modifying a rewrite rule so that it can be accessible from movies.json. If I navigate to http://myurl.com/movies.json I can see the json file with the content expected but if I make a call with AJAX it would return "404 Not Found".
Here is my code for rewrite url:
public function rewrite_url() { global $wp_rewrite; add_rewrite_tag( '%movies%', '([^&]+)' ); add_rewrite_rule( 'movies.json', 'index.php?movies=all', 'top' ); } And here is the code where I output the json file:
public function json_output() { global $wp_query; $movies_tag = $wp_query->get( 'movies' ); if ( ! $movies_tag ) { return; } $movies_array = array(); $args = array( 'post_type' => 'movies', 'posts_per_page' => 100, ); $movies_query = new WP_Query($args); if ( $movies_query->have_posts() ) : while ( $movies_query->have_posts() ) : $movies_query->the_post(); $post_id = get_the_ID(); $movies_array['data'][] = array( 'id' => $post_id, 'title' => get_the_title(), 'poster_url'=> get_post_meta($post_id, 'poster_url', true), 'rating' => get_post_meta($post_id, 'rating', true), 'year' => get_post_meta($post_id, 'year', true), 'short_description' => get_post_meta($post_id, 'short_description', true) ); endwhile; wp_reset_postdata(); endif; header("Access-Control-Allow-Origin: *"); header( 'Content-Type: application/json;' ); wp_send_json( $movies_array ); } Any idea why is this happening? Thanks