• I’ve made a custom select query for displaying my posts based on popularity by views. Here is what I came up with however it isn’t displaying anything.

    <?php
    $table_name = $wpdb->prefix . "top_ten";
    		$current_time = gmdate( 'Y-m-d', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
    		$current_date = strtotime ( '-'.$daily_range. ' DAY' , strtotime ( $current_time ) );
    		$current_date = date ( 'Y-m-j' , $current_date );
    $tptn_settings = tptn_read_options();
    	$limit = $tptn_settings['limit'];
    
    $sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status, post_content ";
    		$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
    		$sql .= "AND post_type = 'post' ";
    		$sql .= "AND post_status = 'publish' AND dp_date >= '$current_date' ";
    		$sql .= "GROUP BY postnumber ";
    		$sql .= "ORDER BY sumCount DESC LIMIT $limit";
    
    	$pageposts = $wpdb->get_results($sql);
    ?>
    <?php if ($pageposts):
    ?>  
    
    <?php global $post; ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
    
    -<-POST CONTENT REMPOVED -->
    
    <?php endforeach; ?>
    <?php endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • I would remove the constraints and run the query. If you get results, then add the constraints back one by one until you find the one that choked.

    Thread Starter markey101101

    (@markey101101)

    Remove the constraints?

    Remove these lines:
    $sql .= “AND post_type = ‘post’ “;
    $sql .= “AND post_status = ‘publish’ AND dp_date >= ‘$current_date’ “;

    That will leave you with just one constraint:
    postnumber=ID

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Select Query for Displaying Posts Not Working’ is closed to new replies.