How to get comments by post ID?

You can use get_comments. Function Reference/get comments $args = array(‘cat’ => ‘home’,’post_type’ => ‘post’)); $post_obj = new WP_Query($args); while($post_obj->have_posts() ) : $post_obj->the_post(); //display comments $comments = get_comments(array( ‘post_id’ => $post->ID, ‘number’ => ‘2’ )); foreach($comments as $comment) { //format comments } endwhile;

Count & Display Database Queries

You can paste this block of code in your currently active WordPress theme functions.php file: function wpse_footer_db_queries(){ echo ‘<!– ‘.get_num_queries().’ queries in ‘.timer_stop(0).’ seconds. –>’.PHP_EOL; } add_action(‘wp_footer’, ‘wpse_footer_db_queries’); The above block of code, will render an HTML comment in the footer of your theme (before </body> and </html>, containing the number of database queries and … Read more

How to query for most viewed posts and show top 5

View this section of the Codex to learn how to create a custom query: http://codex.wordpress.org/Class_Reference/WP_Query Your query will be something like: $query = new WP_Query( array( ‘meta_key’ => ‘post_views_count’, ‘orderby’ => ‘meta_value_num’, ‘posts_per_page’ => 5 ) ); By default, the ordering will be highest to lowest, thus giving you the “top” 5.

get_results using wpdb

global $wpdb; $result = $wpdb->get_results ( ” SELECT * FROM $wpdb->posts WHERE post_type=”page” ” ); foreach ( $result as $page ) { echo $page->ID.'<br/>’; echo $page->post_title.'<br/>’; }

Retrieve posts by term id custom query

Have you tried using the WP_Query class? You might find it’s easier to use the built-in tools for this instead of a custom query from scratch. Something similar to the following should work for you: <?php $args = array( ‘post_type’ => ‘recipe_cpt’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘recipe_tx’, ‘field’ => ‘term_id’, ‘terms’ => 37 … Read more