We have issue where at times the Connect To metabox is running out of memory. It's unbound query that we cannot ship to production with our current hosting provider. Happy to help work on a solution.
Here is the function:
/**
* Query posts for field options.
*
* @param array $field Field settings.
* @return array Field options array.
*/
public static function query( $field ) {
$args = wp_parse_args(
$field['query_args'],
array(
'post_type' => $field['post_type'],
'post_status' => 'publish',
'posts_per_page' => -1,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
)
);
// Get from cache to prevent same queries.
$cache_key = md5( serialize( $args ) );
$options = wp_cache_get( $cache_key, 'meta-box-post-field' );
if ( false !== $options ) {
return $options;
}
$query = new WP_Query( $args );
$options = array();
foreach ( $query->posts as $post ) {
$options[ $post->ID ] = array_merge(
array(
'value' => $post->ID,
'label' => $post->post_title,
'parent' => $post->post_parent,
),
(array) $post
);
}
// Cache the query.
wp_cache_set( $cache_key, $options, 'meta-box-post-field' );
return $options;
}