admin管理员组文章数量:1126077
Currently, I am using the pre_get_posts
action to create a custom query, ordering the posts based on a meta value like so:
add_action( 'pre_get_posts', 'my_custom_order' );
function my_custom_order( $query ) {
if ( ! is_admin() && $query->is_main_query() ) :
$query->set( 'meta_key', '_my_meta_key' );
$query->set( 'orderby', 'meta_value_num' );
endif;
}
which works as expected.
As a next step, I want to use the meta values from another site in the same multisite network, not the values from the current site.
I have tried to switch blogs
function my_custom_order( $query ) {
if ( ! is_admin() && $query->is_main_query() ) :
switch_to_blog( 1 );
$query->set( 'meta_key', '_my_meta_key' );
$query->set( 'orderby', 'meta_value_num' );
restore_current_blog();
endif;
}
which does not work.
Is there any way to achieve this, given the posts exist on both sites?
I do not want to query the posts from blog ID 1, just the order of the posts.
I am aware that synchronizing the meta data between the blogs would be another option, however, I would like to keep the data separated in order to be able to restore the order later on.
本文标签: How can meta values from another site in a multisite be used in a custom query
版权声明:本文标题:How can meta values from another site in a multisite be used in a custom query? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736671289a1946937.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论