WordPress.org

Make WordPress Core

Opened 22 months ago

Closed 16 months ago

Last modified 13 months ago

#22102 closed defect (bug) (duplicate)

Tags list not displaying programmatically added child tags

Reported by: capcar Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4.2
Component: Administration Keywords:
Focuses: Cc:

Description

Greetings,

I have an obscure problem in the admin area that may be a bug. I am migrating a site to WordPress, and am creating a number of hierarchical taxonomies programmatically using the wp_insert_term() function. An example:

Parent 1

---child 1

---child 2

Parent 2

---child 3

---child 4

Parent 3

---child 5

---child 6

I am importing these all at once via code on a hidden page. The tags are all being imported correctly, because they are in the database, they are showing up on the "Edit Post" page in their appropriate panel (properly hierarchical), they can be searched for in the tag admin list, and the admin lists the correct number of tags - in this example 9.

HOWEVER, when I go to the admin tag list, this is the only thing that shows:

Parent 1

---child 1

---child 2

Parent 2

Parent 3

..and yet the the admin lists 9 tags as being in the system. Troubleshooting this, I discovered that if import the children tags separately and REFRESH THE TAG LIST between each import, then the list will show up correctly.

Here's another twist: If I import enough tags to span multiple admin pages (i.e. page 1 of 10), and the last 5 pages are children tags of, in this example, Parent 3... then Wordpress will display 5 empty pages for the list of tags. It clearly knows how many pages are needed for the tags, but it is not displaying them.

I have tested this with the default categories as well as custom taxonomies (hierarchical), and can recreate the problem over and over. This leads me to believe that there is a bug in how WordPress refreshes the tag list - clearly it is not generating this list directly from the database, because those tags are there. Is this list being cached somewhere?

Obscure problem, I know, but hopefully this might ring a bell with someone.

Change History (9)

comment:1 SergeyBiryukov22 months ago

Some code (as simple as possible) to reproduce the issue would be helpful.

comment:2 capcar21 months ago

I'm grabbing keywords from an MSSQL database ($conn) and making them child tags of a set of tags I've set up manually in Wordpress (those are the ID numbers in the functions). Here's the basic code:

import_keywords($conn,'Business',10);
import_keywords($conn,'Entertainment',11);
import_keywords($conn,'Environment',12);
import_keywords($conn,'Finances',13);
import_keywords($conn,'House',14);

function import_keywords($conn,$legacyparent,$parent) {
	if ($conn) {
		$query = "Select * from Keyword where KeywordCategory = '" . $legacyparent . "'";
		$result = odbc_exec($conn,$query);
		echo '<p>Getting keywords for <b>' . $legacyparent . '</b>:<br />';
			while(odbc_fetch_row($result))
				{		
					$term = odbc_result($result,"KeywordText");
					wp_insert_term ($term,'post_tag',array('parent'=> $parent));
					echo '&nbsp;&nbsp;&nbsp;Keyword: <strong>' . $term . '</strong> inserted as child of <strong>' . $legacyparent . '</strong><br />';
				};
				
			odbc_close ($conn);
		} else {
			echo "odbc not connected";
		};
	};
};

The reproducible problem is that if I run all 5 of those functions at once, then the child keywords of the last 4 functions will not appear in the admin menu (even though they are in the database). However, if I run each function independently and refresh the admin menu between each one, then they all appear.

Last edited 21 months ago by capcar (previous) (diff)

comment:3 capcar21 months ago

One thing to note is that in this situation, I've altered the default 'post_tag' taxonomy to be hierarchical in my functions file:

register_taxonomy(
			'post_tag',
			array( 'post','page' ),
			array(
				'hierarchical' => true,
				'labels' => array(
					'name' 			=> _x('Tags', 'taxonomy general name'),
					'singular_name' 	=> _x('Tag', 'taxonomy general name'),
					'search_items' 	=> __('Search Tags'),
					'popular_items' 	=> __('Popular Tags'),
					'all_items' 		=> __('All Tags'),
					'parent_item' 		=> __('Parent Tag'),
					'parent_item_colon' => __('Parent Tag:'),
					'edit_item'		=> __('Edit Tag'),
					'update_item' 		=> __('Update Tag'),
					'add_new_item' 	=> __('Add New Tag'),
					'new_item_name' 	=> __('New Tag Name')
				),
				'query_var' => true,
				'rewrite' => array( 'slug' => 'post_tag' ),
			)
		);


If I switch it back to non-hierarchical, then all tags appear properly in the admin menu.

Version 0, edited 21 months ago by capcar (next)

comment:4 capcar21 months ago

I just discovered that if you edit and save any keyword once, then all of the missing keywords will finally appear on the list. The problem does seem to be that "wp_insert_term" is missing a step when regenerating the Tag list. It's above my head to determine where that step is, however.

comment:5 tarasm16 months ago

I encountered this problem several time as well. I'm looking for a solution for it right now.

comment:6 tarasm16 months ago

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #14485.

comment:7 ocean9014 months ago

  • Milestone Awaiting Review deleted

comment:8 ZaneMatthew13 months ago

  • Cc zanematthew@… added

I've ran into this issue more than one, have yet to find a solutions.

comment:9 ZaneMatthew13 months ago

This snippet from the ticket listed above did the fix

if ( taxonomy_exists( 'my-term' ) ) {
    wp_cache_set( 'last_changed', time( ) - 1800, 'terms' );
    wp_cache_delete( 'all_ids', 'my-term' );
    wp_cache_delete( 'get', 'my-term' );
    delete_option( "my-term_children" );
    _get_term_hierarchy( 'my-term' );
}
Note: See TracTickets for help on using tickets.