WP_Query in plugin causing post title issues
-
I’m building my first plugin to add some custom meta boxes to the edit page of a custom post type called ‘tracks’.
It seems when I use WP_Query to create an array of post titles (these come from ‘artists’ which are also custom post types), some strange things happen; the title field becomes populated with one of the artist names (the last one), and the permalink info becomes visible and suggests that the post is now incorrectly defined as an ‘artist’ with an address such as mysite/artists/artist-name.
I’m not quite sure what’s going on, tried using wp_reset_postdata() and wp_reset_query() after the while loop but that didn’t change anything.
Any help greatly appreciated, here’s my code
$artistList = array(); $genresList = array(); add_action( 'add_meta_boxes', 'getArtists' ); add_action( 'add_meta_boxes', 'getGenres' ); add_action( 'add_meta_boxes', 'cd_meta_box_add' ); function getArtists() { global $post, $artistList; $loop = new WP_Query( array( 'post_type' => 'artists' ) ); while ( $loop->have_posts() ) : $loop->the_post(); array_push($artistList, get_the_title()); endwhile; wp_reset_postdata(); wp_reset_query(); } function getGenres() { global $genresList; $terms = get_terms('genres'); $count = count($terms); if ( $count > 0 ){ foreach ( $terms as $term ) { array_push($genresList, $term->name); } } } function cd_meta_box_add() { add_meta_box( 'artist-mb', 'Artist', 'initArtistMb', 'tracks', 'normal', 'high' ); add_meta_box( 'vimeoLink-mb', 'Vimeo Link', 'initVimeoMb', 'tracks', 'normal', 'high' ); add_meta_box( 'youtubeLink-mb', 'YouTube Link', 'initYoutubeMb', 'tracks', 'normal', 'high' ); add_meta_box( 'genre-mb', 'Genre', 'initGenreMb', 'tracks', 'normal', 'high' ); } function initArtistMb() { global $artistList; echo '<select name="name" id="artist-combo">'; $count = sizeof($artistList); echo '<option>Select an Artist...</option>'; for($i=0; $i<$count; $i++) { echo '<option>', $artistList[$i], '</option>'; } echo '</select>'; } function initVimeoMb() { ?> <label for="my_meta_box_text">URL:</label> <input type="text" name="vimeo_link" id="vimeo_link" /> <?php } function initYoutubeMb() { ?> <label for="my_meta_box_text">URL:</label> <input type="text" name="youtube_link" id="youtube_link" /> <?php } function initGenreMb() { global $genresList; echo '<select name="name" id="genre-combo">'; $count = sizeof($genresList); echo '<option>Select a Genre...</option>'; for($i=0; $i<$count; $i++) { echo '<option>', $genresList[$i], '</option>'; } echo '</select>'; }
The topic ‘WP_Query in plugin causing post title issues’ is closed to new replies.