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