简体   繁体   中英

Increase query performance in wp_posts, via wp_get_nav_menu_items using custom sql query in wordpress

Today I ran into a problem that noticeably slows down the performance of the site. In my header, menu categories are loaded (menu name -> header_nav). I load them with a function like wp_get_nav_menu_items ($ menu-> term_id). This function returns an array of objects in which there are a lot of fields, since there are a lot of categories, there are also a lot of objects. From the whole object, I need only $ object-> title, $ object-> link, $ object-> id. I need to get this data from multilingual titles.

//$menu_items too large array of objects

$menu = wp_get_nav_menu_object( $locations[ 'menu_slug'] );

//Here my array. do foreach and creating category menu using  
//$menu_item->title...and etc.
$menu_items = wp_get_nav_menu_items($menu->term_id);  

And I decided to write my own request. But it returns data without multilingual

    SELECT p2.ID, p2.post_title, p2.post_name, p2.guid, 
    p1.post_parent
    FROM wp_posts p1
    INNER JOIN wp_term_relationships AS TR
    ON TR.object_id = p1.ID
    INNER JOIN wp_postmeta AS pm
    ON pm.post_id = p1.ID
    INNER JOIN wp_posts AS p2
    ON p2.ID = pm.meta_value
    WHERE p1.post_type = 'nav_menu_item'
    AND TR.term_taxonomy_id = $taxId
    AND pm.meta_key = '_menu_item_object_id'
    ORDER BY p1.menu_order ASC

How to return already translated titles?

 SELECT * FROM wp_posts p1 INNER JOIN wp_term_relationships AS TR ON TR.object_id = 
 p1.ID INNER JOIN wp_postmeta AS PM ON pm.post_id = p1.ID INNER JOIN wp_posts AS p2 ON 
 p2.ID = PM.meta_value WHERE p1.post_type = 'nav_menu_item' AND TR.term_taxonomy_id = ( 
 SELECT wp_terms.term_id FROM wp_terms WHERE slug = 'pedik') AND pm.meta_key = 
 '_menu_item_object_id' ORDER BY p1.menu_order ASC

for title

 $menu_item->object_id = ! isset( $menu_item->object_id ) ? get_post_meta( 
 $menu_item- 
 >ID, '_menu_item_object_id', true ) : $menu_item->object_id;

for url

 $menu_item->url = get_permalink( $menu_item->object_id );

for title

 $original_title = apply_filters( 'the_title', $original_object- 
 >post_title,$original_object->ID );
 if ( '' === $original_title ) {
                /* translators: %d: ID of a post */
                $original_title = sprintf( __( '#%d (no title)' ), $original_object- 
 >ID );
            }

            $menu_item->title = '' == $menu_item->post_title ? $original_title : 
 $menu_item->post_title;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM