我已经找过这个问题,但我以前没有看到过这个问题。如果这是一个重复的问题,我很抱歉。
我已经对我的自定义数据库表进行了复杂的查询,现在我有一个 wordpress 帖子 ID 数组。
现在我想用the_loop向他们展示,但我做不到。我认为重要的是要通知您,我正在通过ajax显示结果,因此所有这些功能都在函数中.php(也许此信息无关紧要)。
现在,我正在通过以下方式获取帖子数据:
function imprimirResultados($resultados = null){
if ($resultados){
echo '<div class="resultados">';
foreach ($resultados as $post_id){
$post = get_post( $post_id[0], ARRAY_A );
echo '<div class="post" id="post-'.$post['ID'].'">
<h2><a href="'.$post['guid'].'" rel="bookmark" title="Permanent Link to '.$post['post_name'].'">
'.$post['post_title'].'</a></h2>
<div class="entry">
'.$post['post_excerpt'].'
</div>
</div>';
}
echo '</div>';
var_dump($post);
}else{
echo '<h2 class="center">No encontrado</h2>
<p class="center">Lo sentimos, pero la búsqueda no obtuvo resultados.</p>';
}
}
但是这不是一个干净的方法,我不能使用 the_loop() 生成的对象已经具有的其他功能。
---------------------------------编辑-------------------------------
我在这里留下我使用的功能:
function resultadosLigeros_callback(){
(...)
$querystr = cargar_ligeros($marca,$modelo,$combustible,$tipo,$anio); //This function generate a MySQL query
$resultados = $wpdb->get_results($querystr, ARRAY_N);
imprimirResultados($resultados, $querystr);//This function is the one I wrote before
die();
}
您需要根据
您拥有的帖子 ID 生成新WP_Query。具体来说,您需要使用post__in。
例如,由于$resultados
是一个数组:
<?php
// Fetch the posts
$query = new WP_Query( array( 'post__in' => $resultados ) );
// The Loop
while ( $query->have_posts() ) : $query->the_post();
?>
然后,您可以直接在模板逻辑中使用 the_ID()
等函数。