Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v0.0.2 2022-08-08

### Added

- `composer.json` to make the plugin ready to be installed via composer
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "builtbycactus/total-counts-for-wp-graphql",
"description": "Adds the ability to fetch total post counts from WP-GraphQL",
"type": "wordpress-plugin",
"license": "GNU General Public License v2.0",
"version": "0.0.3",
"authors": [
{
"name": "Built By Cactus",
"homepage": "https://builtbycactus.co.uk/"
}
]
}
21 changes: 14 additions & 7 deletions total-counts-for-wp-graphql.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Author URI: https://builtbycactus.co.uk/
* Text Domain: cactus-gqltc
* Domain Path: /languages
* Version: 0.0.2
* Version: 0.0.3
*/

namespace Cactus\GQLTC;
Expand Down Expand Up @@ -73,13 +73,20 @@ function () {
*/
function resolve_total_field( $page_info, $connection ) {
$page_info['total'] = null;
if ( $connection->get_query() instanceof \WP_Query ) {
if ( isset( $connection->get_query()->found_posts ) ) {
$page_info['total'] = (int) $connection->get_query()->found_posts;

// Change the query property to be public then access it.
$reflector = new \ReflectionObject( $connection );
$property = $reflector->getProperty( 'query' );
$property->setAccessible( true );
$connectionQuery = $property->getValue( $connection );

if ( $connectionQuery instanceof \WP_Query ) {
if ( isset( $connectionQuery->found_posts ) ) {
$page_info['total'] = (int) $connectionQuery->found_posts;
}
} elseif ( $connection->get_query() instanceof \WP_User_Query ) {
if ( isset( $connection->get_query()->total_users ) ) {
$page_info['total'] = (int) $connection->get_query()->total_users;
} elseif ( $connectionQuery instanceof \WP_User_Query ) {
if ( isset( $connectionQuery->total_users ) ) {
$page_info['total'] = (int) $connectionQuery->total_users;
}
}

Expand Down