使用wordpress中的自定义SQL查询通过wp_get_nav_menu_items提高wp_posts中的查询性能
今天, 我遇到了一个问题, 该问题显着降低了网站的性能。在我的标题中, 菜单类别已加载(菜单名称->
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
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;
【使用wordpress中的自定义SQL查询通过wp_get_nav_menu_items提高wp_posts中的查询性能】网址
$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;
推荐阅读
- 缩进按钮和WordPress可视编辑器中的无序列表
- 在自定义.php文件中包含wp-load.php会导致页面处理停止。为什么呢
- 在WordPress主题中包含Gutenberg块
- Sorry, name can no longer contain capital letters.
- SpringBoot集成RabbitMQ
- -save-dev 与 -save的区别
- oracle拨云见日第3篇之安装11g
- PowerDesigner使用
- el-date-picker日期选择控件少一天的问题