diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..945c5b9 --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..750da37 --- /dev/null +++ b/composer.json @@ -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/" + } + ] +} diff --git a/total-counts-for-wp-graphql.php b/total-counts-for-wp-graphql.php index 8c4d4d9..a98fd03 100644 --- a/total-counts-for-wp-graphql.php +++ b/total-counts-for-wp-graphql.php @@ -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; @@ -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; } }