我已尝试过以下链接中的解决方案,但是,我不能让它们显示帖子!
http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
http://wordpress.stackexchange.com/questions/1529/displaying-posts-depending-on-number-of-comments-and-latest-comments
http://wordpress.org/support/topic/sort-by-number-of-comments
这是我的默认代码,我的问题是,如何通过注释计数对此代码进行排序?
<?php
/*
Template Name: Homepage
*/
get_header(); global $gp_settings, $user_ID, $user_identity, $user_level;
$referrer = $_SERVER['HTTP_REFERER'];
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- BEGIN CONTENT -->
<div id="content">
<!-- BEGIN POST CONTENT -->
<?php if($post->post_content) { ?>
<div id="post-content">
<?php the_content(__('Read More »', 'gp_lang')); ?>
</div>
<?php } ?>
<!-- END POST CONTENT -->
</div>
<!-- END CONTENT -->
<?php endwhile; endif; ?>
<?php get_footer(); ?>
以下是我尝试过的代码示例
<?php
/*
Template Name: Homepage
*/
get_header(); global $gp_settings, $user_ID, $user_identity, $user_level;
$referrer = $_SERVER['HTTP_REFERER'];
?>
<?php
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'tag'
AND $wpdb->postmeta.meta_value = 'email'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->posts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<!-- BEGIN CONTENT -->
<div id="content">
<!-- BEGIN POST CONTENT -->
<?php if($post->post_content) { ?>
<div id="post-content">
<?php the_content(__('Read More »', 'gp_lang')); ?>
</div>
<?php } ?>
<!-- END POST CONTENT -->
</div>
<!-- END CONTENT -->
<?php endforeach; ?>
<?php endif; ?>
<?php get_footer(); ?>
答案 0 :(得分:0)
经验法则:如果您正在针对默认的Wordpress表运行SQL查询,那么您可能做错了。
假设页面原始查询中出现的帖子是正确的,您只想修改它们的顺序,您可以使用主循环的原始参数构建一个新的Query并循环通过它:
<?php
/*
Template Name: Homepage
*/
get_header(); global $gp_settings, $user_ID, $user_identity, $user_level;
$referrer = $_SERVER['HTTP_REFERER'];
global $wp_query; //Gets the default query
$default_args = $wp_query->query; //Grabs the original arguments
/*
If you find that the above line returns a string rather than an array,
use this instead:
*/
//parse_str($wp_query->query, $default_args);
$default_args['orderby'] = 'comment_count'; //Set the new order parameter
$q = new WP_Query($default_args); //Generate a new Query
if ($q->have_posts()) : while ($q->have_posts()) : $q->the_post(); //Loop through new Query ?>
<!-- BEGIN CONTENT -->
<div id="content">
<!-- BEGIN POST CONTENT -->
<?php if($post->post_content) { ?>
<div id="post-content">
<?php the_content(__('Read More »', 'gp_lang')); ?>
</div>
<?php } ?>
<!-- END POST CONTENT -->
</div>
<!-- END CONTENT -->
<?php
endwhile; endif;
get_footer();
?>