From d7c22c53f8e8272f8005278298c64998dc98edc2 Mon Sep 17 00:00:00 2001 From: Paul Schreiber Date: Mon, 28 Aug 2023 21:13:30 -0400 Subject: [PATCH] fix: replace get_page_by_title() with WP Query * rename $parent to $parent_element --- includes/class-terraso-cli.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/includes/class-terraso-cli.php b/includes/class-terraso-cli.php index d55e64d..ee38d63 100644 --- a/includes/class-terraso-cli.php +++ b/includes/class-terraso-cli.php @@ -18,17 +18,34 @@ class Terraso_CLI extends WP_CLI_Command { /** * Insert the outputs in to the database. * - * @param string $parent The ILM element (output) these outputs (tools) belong to. + * @param string $parent_element The ILM element (output) these outputs (tools) belong to. * @param array $data Array of element titles and contents. * @param string $type Item type (output or tool). * @param bool $dry_run If performing a try run. */ - public static function import_data( $parent, $data, $type, $dry_run ) { + public static function import_data( $parent_element, $data, $type, $dry_run ) { + + $query = new WP_Query( + [ + 'post_type' => self::POST_TYPE, + 'title' => $parent_element, + 'post_status' => 'all', + 'numberposts' => 1, + 'update_post_term_cache' => false, + 'update_post_meta_cache' => false, + 'orderby' => 'date', + 'order' => 'ASC', + ] + ); - $post_parent = get_page_by_title( $parent, OBJECT, self::POST_TYPE ); + if ( ! empty( $query->post ) ) { + $post_parent = $query->post; + } else { + $post_parent = null; + } if ( ! $post_parent ) { - WP_CLI::error( "Could not find post for parent item {$parent}" ); + WP_CLI::error( "Could not find post for parent item {$parent_element}" ); } foreach ( $data as $item ) {