我有一个自定义的元查询,速度非常慢,甚至直到最后才加载。最多三个arrays
在里面\'meta_query\'
查询工作正常,如果有四个或更多,它就不再工作了。
在搜索我找到的原因时this post 但我绝对不熟悉自定义db查询。
非常感谢您的帮助!非常感谢。
<?php
$args = array(
\'post_type\' => $post_type,
\'posts_per_page\' => -1,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\'=>\'_author\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
),
array(
\'key\'=>\'_publisher\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
),
array(
\'key\'=>\'_contributor_1\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
),
array(
\'key\'=>\'_contributor_2\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
),
array(
\'key\'=>\'_contributor_3\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ) : while( $posts->have_posts() ) : $posts->the_post(); ?>
<li><a href="<?php echo get_the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>
––––
更新了代码,添加了boger:
页php
<?php
$args = array(
\'post_type\' => $post_type,
\'posts_per_page\' => -1,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\'=>\'_author\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
),
array(
\'key\'=>\'_publisher\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
),
array(
\'key\'=>\'_contributor_1\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
),
array(
\'key\'=>\'_contributor_2\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
),
array(
\'key\'=>\'_contributor_3\',
\'value\'=> $author_single["fullname"],
\'compare\' => \'=\'
)
)
);
add_filter( \'posts_clauses\', \'wpse158898_posts_clauses\', 10, 2 );
$posts = new WP_Query($args);
if( $posts->have_posts() ) : while( $posts->have_posts() ) : $posts->the_post(); ?>
<li><a href="<?php echo get_the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif;
remove_filter( \'posts_clauses\', \'wpse158898_posts_clauses\', 10 ); ?>
功能。php
function wpse158898_posts_clauses( $pieces, $query ) {
global $wpdb;
$relation = isset( $query->meta_query->relation ) ? $query->meta_query->relation : \'AND\';
if ( $relation != \'OR\' ) return $pieces; // Only makes sense if OR.
$prepare_args = array();
$key_value_compares = array();
foreach ( $query->meta_query->queries as $meta_query ) {
// Doesn\'t work for IN, NOT IN, BETWEEN, NOT BETWEEN, NOT EXISTS.
if ( ! isset( $meta_query[\'value\'] ) || is_array( $meta_query[\'value\'] ) ) return $pieces; // Bail if no value or is array.
$key_value_compares[] = \'(pm.meta_key = %s AND pm.meta_value \' . $meta_query[\'compare\'] . \' %s)\';
$prepare_args[] = $meta_query[\'key\'];
$prepare_args[] = $meta_query[\'value\'];
}
$sql = \' JOIN \' . $wpdb->postmeta . \' pm on pm.post_id = \' . $wpdb->posts . \'.ID\'
. \' AND (\' . implode( \' \' . $relation . \' \', $key_value_compares ) . \')\';
array_unshift( $prepare_args, $sql );
$pieces[\'join\'] = call_user_func_array( array( $wpdb, \'prepare\' ), $prepare_args );
$pieces[\'where\'] = preg_replace( \'/ AND[^w]+wp_postmeta.*$/s\', \'\', $pieces[\'where\'] ); // Zap postmeta clauses.
return $pieces;
}
–––
$posts->request
输出
$args = array(
\'post_type\' => $post_type,
\'posts_per_page\' => -1,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\'=>\'_author\',
\'value\'=> "Hanna Meier",
\'compare\' => \'=\'
),
array(
\'key\'=>\'_publisher\',
\'value\'=> "Friedhelm Peters",
\'compare\' => \'=\'
)
)
);
没有自定义查询
SELECT wp_vacat_posts.* FROM wp_vacat_posts INNER JOIN wp_vacat_postmeta ON (wp_vacat_posts.ID = wp_vacat_postmeta.post_id)
INNER JOIN wp_vacat_postmeta AS mt1 ON (wp_vacat_posts.ID = mt1.post_id) WHERE 1=1 AND wp_vacat_posts.post_type = \'product\' AND (wp_vacat_posts.post_status = \'publish\' OR wp_vacat_posts.post_status = \'private\') AND ( (wp_vacat_postmeta.meta_key = \'_author\' AND CAST(wp_vacat_postmeta.meta_value AS CHAR) = \'Hanna Meier\')
OR (mt1.meta_key = \'_publisher\' AND CAST(mt1.meta_value AS CHAR) = \'Friedhelm Peters\') ) GROUP BY wp_vacat_posts.ID ORDER BY wp_vacat_posts.post_date DESC
使用自定义查询
SELECT wp_vacat_posts.* FROM wp_vacat_posts
JOIN wp_vacat_postmeta pm on pm.post_id = wp_vacat_posts.ID AND ((pm.meta_key = \'_author\' AND pm.meta_value = \'Hanna Meier\') OR (pm.meta_key = \'_publisher\' AND pm.meta_value = \'Friedhelm Peters\')) WHERE 1=1 AND wp_vacat_posts.post_type = \'product\' AND (wp_vacat_posts.post_status = \'publish\' OR wp_vacat_posts.post_status = \'private\') AND ( (wp_vacat_postmeta.meta_key = \'_author\' AND CAST(wp_vacat_postmeta.meta_value AS CHAR) = \'Hanna Meier\')
OR (mt1.meta_key = \'_publisher\' AND CAST(mt1.meta_value AS CHAR) = \'Friedhelm Peters\') ) GROUP BY wp_vacat_posts.ID ORDER BY wp_vacat_posts.post_date DESC