使用wordpress中的自定义sql查询,通过wp_get_nav_menu_items提高wp_posts中的查询性能

问题描述 投票:0回答:1

今天我遇到了一个明显降低网站性能的问题。在我的标题中,加载了菜单类别(菜单名称 - > header_nav)。我使用像wp_get_nav_menu_items($ menu-> term_id)这样的函数加载它们。这个函数返回一个对象数组,其中有很多字段,因为有很多类别,还有很多对象。从整个对象,我只需要$ object-> title,$ object-> link,$ object-> id。我需要从多语言标题中获取这些数据。

//$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);  

我决定写自己的请求。但它没有多语言就返回数据

    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

如何返回已翻译的标题?

sql wordpress performance wordpress-theming
1个回答
0
投票
 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

标题

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

为网址

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

标题

 $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;
© www.soinside.com 2019 - 2024. All rights reserved.