Currently I’m running a MySQL query like this
$query = "
SELECT
t1.id AS 'post_id',
t1.post_title AS 'post_title',
t1.post_status AS 'post_status',
COUNT( t1.id ) AS 'row_count', //this one is not working
COUNT( t2.tag_id ) AS 'link_count',
GROUP_CONCAT( t2.tag_id) AS 'link_id',
GROUP_CONCAT( t2.url) AS 'url',
GROUP_CONCAT( t2.title) AS 'title',
GROUP_CONCAT( t2.active) AS 'active',
FROM $posts_table AS t1
JOIN $tags_table AS t2 ON ( t1.id = t2.post_id )
GROUP BY t1.id
ORDER BY t1.id DESC
LIMIT $start_from, $row_to_show;
";
Every part of this query is executing well except COUNT( t1.id ) AS 'row_count', line.
I want to count total numbers of rows found in t1 table but when I’m dumping the array
$result = $wpdb->get_results($query);
var_dump($result->row_count);
It is giving me NULL.
If I use it inside a loop
foreach ( $result as $result ){
echo $result->row_count;
}
Then the value is same as link_count that is declared in the very next line COUNT( t2.link_id ) AS 'link_count', gives the value I want.
Please give me some idea about how I can use pagination (like 10 out of 30 results) on this query.
I tried many but didn’t succeed so I made another separate DB query for just pagination.
it gives me the result of total numbers of rows that I can use for pagination.