[英]Get Wordpress posts so things like get_post_thumbnail_id don't make a database call
有沒有一種方法可以提取Wordpress帖子,以使meta調用實際上不進行數據庫調用?
例如,我目前有:
$args = array(
'posts_per_page' => 1000,
'post_type' => 'portfoliosample'
);
$query = new WP_Query($args);
while($query->have_posts()):
$query->the_post();
$style = '';
if(has_post_thumbnail($post->ID)){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$largeurl = $image[0];
$style = ' style="background-image:url('.$largeurl.');"';
}
?>
<li id="post-<?php the_ID(); ?>" <?php post_class('half'); ?>>
<a href="<?php the_permalink()?>"<?php echo $style?> ng-click="lightbox('portfolio','<?php the_permalink()?>')">
<h3 class="entry-title caps"><?php echo strtoupper(the_title('', '', false)); ?></h3>
</a>
</li>
<?php
endwhile;
諸如the_title()
之類的the_title()
不會進行數據庫調用,因為post
具有該信息。 但是, $post
似乎沒有帖子的縮略圖ID或該縮略圖的圖像源。
如果我這樣進行自定義查詢:
global $wpdb;
$query = "
SELECT posts.*
FROM $wpdb->posts posts
WHERE posts.post_type = 'portfoliosample'
AND posts.post_status = 'publish'
ORDER BY posts.menu_order ASC
";
$pageposts = $wpdb->get_results($query, OBJECT);
if($pageposts)
{
global $post;
foreach($pageposts as $post)
{
setup_postdata($post);
$style = '';
if(has_post_thumbnail($post->ID)){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$largeurl = $image[0];
$style = ' style="background-image:url('.$largeurl.');"';
}
?>
<li id="post-<?php the_ID(); ?>" <?php post_class('half'); ?>>
<a href="<?php the_permalink()?>"<?php echo $style?> ng-click="lightbox('portfolio','<?php the_permalink()?>')">
<h3 class="entry-title caps"><?php echo strtoupper(the_title('', '', false)); ?></h3>
</a>
</li><!-- #post-## -->
<?php
}
}
我可以同時獲取元數據,但仍然允許get_post_thumbnail()
類的功能嗎? 我希望setup_postdata()
有辦法解決這個問題。
沒有一種方法可以做我想要的。 您要么必須手動執行這些功能要做的所有事情,要么允許它們進行數據庫調用。
嘗試以下操作(未經測試,可能會有點破損,但應該可以使您朝正確的方向前進):
$querystr = "
SELECT $wpdb->posts.*, $wpdb->postmeta.*
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON {$wpdb->postmeta}.id = {$wpdb->post}.id
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.key = '_thumbnail_id'
AND $wpdb->posts.post_type = 'portfoliosample'
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
然后像這樣循環遍歷它:
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endforeach; ?>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
您必須從$ post獲取實際的元值,而不是使用WP post元函數,但是至少您將在一個查詢中獲得數據。
如果要進一步使用它並獲取完整的文件信息,則必須進行其他聯接(再次,我確定它的書寫方式不正確,但是它將為您提供正確的想法,而不是*您是我只想獲取您需要的字段)
$querystr = "
SELECT post.*, pm.*, attach.*
FROM $wpdb->posts post
LEFT JOIN $wpdb->postmeta pm ON pm.id = {$wpdb->post}.id
LEFT JOIN $wpdb->posts attach ON attach.id = pm.id
WHERE post.post_status = 'publish'
AND pm.key = '_thumbnail_id'
AND post.post_type = 'portfoliosample'
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.