Support » Plugin: Nested Pages » Bug: duplicated menu items (with fix)

  • Resolved Marco Chiesi

    (@marcochiesi)


    Hi there, and thanks for this useful plugin.

    Unfortunately today I noticed a strange behavior with it. The plugin was filling the “Nested Pages” menu with lots of duplicated pages. This was happening even after deleting the “Nested Pages” menu. In facts, every time I was loading the pages list, a new set of duplicated menu items was added to the menu, that become so full of duplicated.

    After some investigation I found this is depending on a bug on a SQL query used by the plugin, which does not handle correctly the term_taxonomy table.
    This bug is probably not very frequent because often the term_id’s are the same as the term_taxonomy_id’s, but that is just a matter of “luck” and it’s a conceptual error to consider them the same when composing SQL queries.

    The offending code is in class NavMenuRepository in method getMenuItem.

    The actual code:

    if ( $query == 'object_id' ){
    			$menu_id = $this->getMenuID();
    			$prefix = $wpdb->prefix;
    			$meta_table = $prefix . 'postmeta';
    			$term_relationships_table = $prefix . 'term_relationships';
    			$terms_table = $prefix . 'terms';
    			$sql = "SELECT
    				pm.post_id,
    				t.term_id,
    				t.name,
    				pmx.meta_value AS xfn_type
    				FROM $meta_table AS pm
    				LEFT JOIN $term_relationships_table AS tr
    				ON tr.object_id = pm.post_id
    				LEFT JOIN $terms_table AS t
    				ON t.term_id = tr.term_taxonomy_id
    				LEFT JOIN $meta_table AS pmx
    				ON pmx.post_id = pm.post_id AND pmx.meta_key = '_menu_item_xfn'
    				WHERE pm.meta_value = $id AND pm.meta_key = '_menu_item_object_id'
    			";
    			$results = $wpdb->get_results($sql);
    			foreach($results as $result){
    				if ( $result->term_id == $menu_id && $result->xfn_type == 'page' ) $post_id = $result->post_id;
    			}
    			return $post_id;
    		}

    should be changed to

    if ( $query == 'object_id' ){
    			$menu_id = $this->getMenuID();
    			$prefix = $wpdb->prefix;
    			$meta_table = $prefix . 'postmeta';
    			$term_relationships_table = $prefix . 'term_relationships';
    			$term_taxonomy_table = $prefix . 'term_taxonomy';
    			$terms_table = $prefix . 'terms';
    			$sql = "SELECT
    				pm.post_id,
    				t.term_id,
    				t.name,
    				pmx.meta_value AS xfn_type
    				FROM $meta_table AS pm
    				LEFT JOIN $term_relationships_table AS tr
    				ON tr.object_id = pm.post_id
    				LEFT JOIN $term_taxonomy_table AS tt
    				ON tt.term_taxonomy_id = tr.term_taxonomy_id
    				LEFT JOIN $terms_table AS t
    				ON t.term_id = tt.term_id
    				LEFT JOIN $meta_table AS pmx
    				ON pmx.post_id = pm.post_id AND pmx.meta_key = '_menu_item_xfn'
    				WHERE pm.meta_value = $id AND pm.meta_key = '_menu_item_object_id'
    			";

    https://wordpress.org/plugins/wp-nested-pages/

Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Bug: duplicated menu items (with fix)’ is closed to new replies.