'wordpress search result order by meta but duplicate two select sql query
I want to customize wordpress search results so that all articles are sorted by a meta value named views field,below is my code:
<?php get_header(); ?>
<div class="content-wrap">
<div class="content">
<?php
wp_reset_query();
wp_reset_postdata();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'showposts' => 100,
'ignore_sticky_posts' => 1,
'paged' => $paged,
's' => $s,
'post_status' => 'publish',
'post_type' => 'post',
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
query_posts($args);
?>
<?php if (!have_posts()) : ?>
<header class="archive-header">
<h1><?php echo sprintf(__('about【%s】', 'TBL'), htmlspecialchars($s)) ?></h1>
<p class="muted"><?php _e('Here u go:', 'TBL') ?></p>
</header>
<?php else : ?>
<header class="archive-header">
<h1 class="s-title"><?php echo sprintf(__('<strong>[%s]</strong> · 相关内容', 'TBL'), htmlspecialchars($s)) ?>
<span style="float: right;">
<?php
global $wp_query;
if ($wp_query->found_posts < 2) {
$result = "result";
} else {
$result = "results";
}
echo $wp_query->found_posts . " " . "条";
?>
</span>
</h1>
</header>
<?php
include('modules/excerpt.php');
?>
<?php endif; ?>
</div>
</div>
<?php get_sidebar();
get_footer(); ?>
But what is confusing is that this code produces two queries,one is
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND (((wp_posts.post_title LIKE '%天使%')
OR (wp_posts.post_excerpt LIKE '%天使%')
OR (wp_posts.post_content LIKE '%天使%')))
AND ((wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private'))
OR (wp_posts.post_type = 'page'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private'))
OR (wp_posts.post_type = 'attachment'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private')))
ORDER BY wp_posts.post_title LIKE '%天使%' DESC, wp_posts.post_date DESC
LIMIT 0, 100
the other one is
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1
AND (((wp_posts.post_title LIKE '%天使%')
OR (wp_posts.post_excerpt LIKE '%天使%')
OR (wp_posts.post_content LIKE '%天使%')))
AND ( wp_postmeta.meta_key = 'views' )
AND wp_posts.post_type = 'post'
AND ((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value+0 DESC
LIMIT 0, 100
WP_Query->get_posts
How can I do to generate only the second sql query ? Because my wordpress has more than one million data, all two sql statements make the search wait time, it's too time consuming. Any help I would appreciate so so so much!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
