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

时间:2019-04-10 20:43:58

标签: sql wordpress performance wordpress-theming

今天,我遇到了一个问题,该问题显着降低了网站的性能。在我的标题中,菜单类别已加载(菜单名称-> header_nav)。我用wp_get_nav_menu_items($ menu-> term_id)之类的函数加载它们。该函数返回一个对象数组,其中有很多字段,因为有很多类别,所以也有很多对象。从整个对象来看,我只需要$ object->标题,$ object->链接,$ 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

如何返回已翻译的标题?

1 个答案:

答案 0 :(得分: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;