WordPress.org

Forums

WP_Query in plugin causing post title issues (6 posts)

  1. r0bbiem
    Member
    Posted 2 years ago #

    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>';
    }
  2. r0bbiem
    Member
    Posted 2 years ago #

    Another update, I still haven't solved this problem, tried replacing the contents of my getArtists() function with this;

    $artistPosts = get_posts('post_type=artists');
    	foreach($artistPosts as $post) :
    	array_push($artistList, get_the_title());
    	endforeach;

    Thought that by avoiding the loop the problems might disappear but my posts still seem to be mis-categorized somehow by the use of this function.

    Anyone out there?

  3. Jeremy Pry
    Member
    Posted 2 years ago #

    Hi r0bbiem,

    Unfortunately I don't have any other information to help, but I've discovered this same problem in a different way. The only workaround I've found is to ditch the WP_Query, but of course that leaves us without being able to do what we need to do.

    Here's the link to the forum topic I just started: http://wordpress.org/support/topic/wp_query-in-admin-bar-breaks-post-editor?replies=1

  4. bcworkz
    Member
    Posted 2 years ago #

    Hey all,
    I don't have an answer either, but this is starting to sound quite strange. There shouldn't be any way to get WP_Query to return something like you are describing, especially since your code appears on a quick read to be perfectly acceptable. But yet somehow your code seems to be causing it, since no one not using your plugins seem to be having an issue. My best guess is there is a name collision or something similar conflicting with other plugins. Neither plugin is doing anything wrong on it's own, but together they are wreaking havoc with something, perhaps the cache?

    You might try disabling all plugins except yours to see if the issue persists. There could also be a conflict with your current theme, so if disabling plugins does not help, you might try swapping themes as well. It will be interesting to see if my half-baked theory bears any fruit.

  5. Jeremy Pry
    Member
    Posted 2 years ago #

    bcworkz,

    Thanks for chiming in, but this isn't a plugin or theme conflict. I did extensive troubleshooting to narrow down the exact source of the issue to the call to WP_Query, and then afterwards discovered this thread of someone else having the same issue, albeit with a slightly different method of using WP_Query.

  6. r0bbiem
    Member
    Posted 2 years ago #

    Thanks for all the input, in the end I solved the problem by moving the array declarations into functions.php and then accessing them as global variables.

    Here's the working code: pastebin.com/vHuCdL5P

    Since then I've also discovered get_option() and update_option() as described here http://bit.ly/aeVE97 and to me this seems like a more robust way of dealing with site-wide variables such as these.

    I've learned a lot through this process, still not clear on what was causing my initial problem but this should help anyone experiencing the same kind of issues.

Topic Closed

This topic has been closed to new replies.

About this Topic