WordPress 查询方式
- 默认主查询: 当加载页面或帖子时,WordPress 自动生成并执行主查询。你可以直接在模板文件中使用它,例如
index.php
、single.php
等。
<?php get_header(); ?>
<section>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div><?php the_excerpt(); ?></div>
</article>
<?php endwhile; ?>
<?php else : ?>
<p>没有找到相关文章。</p>
<?php endif; ?>
</section>
<?php get_footer(); ?>
- 分类存档主查询:
category.php
中的主查询默认会查询该分类下的文章。
<?php get_header(); ?>
<section>
<h2><?php single_cat_title(); ?></h2>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div><?php the_excerpt(); ?></div>
</article>
<?php endwhile; ?>
<?php else : ?>
<p>没有找到相关文章。</p>
<?php endif; ?>
</section>
<?php get_footer(); ?>
- 搜索结果主查询:
search.php
中的主查询默认会根据用户输入的搜索关键词查询文章。
<?php get_header(); ?>
<section>
<h2>搜索结果:<?php echo get_search_query(); ?></h2>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div><?php the_excerpt(); ?></div>
</article>
<?php endwhile; ?>
<?php else : ?>
<p>没有找到相关文章。</p>
<?php endif; ?>
</section>
<?php get_footer(); ?>
- get_posts():
get_posts()
是 WP_Query
的简化版本,用于获取一组文章对象。
<?php get_header(); ?>
<section>
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
);
$posts = get_posts($args);
if ($posts) :
foreach ($posts as $post) :
setup_postdata($post);
?>
<article>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endforeach;
wp_reset_postdata();
else :
?>
<p>没有找到相关文章。</p>
<?php
endif;
?>
</section>
<?php get_footer(); ?>
- query_posts():
query_posts()
是一个用于修改主查询的函数。不推荐用于生产环境,但在某些情况下可以使用。
<?php get_header(); ?>
<section>
<?php
query_posts(array(
'post_type' => 'post',
'posts_per_page' => 5,
));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<article>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div><?php the_excerpt(); ?></div>
</article>
<?php endwhile;
else :
?>
<p>没有找到相关文章。</p>
<?php
endif;
wp_reset_query();
?>
</section>
<?php get_footer(); ?>
- WP_Query:
WP_Query
类是 WordPress 中最常用的查询类,用于查询文章、页面和自定义文章类型。
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
echo '<h3>' . get_the_title() . '</h3>';
echo '<div>' . get_the_excerpt() . '</div>';
endwhile;
wp_reset_postdata();
endif;
- $wpdb: 使用
$wpdb
对象执行自定义 SQL 查询,适用于查询自定义表或复杂查询。
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM your_custom_table");
if ($results) :
foreach ($results as $row) {
echo '<h3>' . esc_html($row->column_name_1) . '</h3>';
echo '<p>' . esc_html($row->column_name_2) . '</p>';
}
else :
echo '<p>没有找到数据。</p>';
endif;
优化查询性能的方法
- 使用缓存: 使用 Transients API 缓存查询结果,以减少数据库查询次数。
$transient_key = 'custom_query_results';
$query_results = get_transient($transient_key);
if (false === $query_results) {
$args = array('post_type' => 'post', 'posts_per_page' => 10);
$query_results = new WP_Query($args);
set_transient($transient_key, $query_results, HOUR_IN_SECONDS);
}
- 限制查询字段: 只获取必要的字段,以减少查询开销。
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'fields' => 'ids' // 只获取文章ID
);
- 批量获取元数据: 预先缓存所有文章的元数据,以减少查询次数。
update_post_meta_cache(wp_list_pluck($wp_query->posts, 'ID'));
- 使用高效的查询参数: 避免不必要的复杂查询,尽量使用简单高效的查询参数。
- 优化数据库: 确保数据库结构和索引合理,定期优化数据库。