Skip to content

Commit

Permalink
fix: replace get_page_by_title() with WP Query
Browse files Browse the repository at this point in the history
* rename $parent to $parent_element
  • Loading branch information
paulschreiber committed Aug 29, 2023
1 parent e160378 commit d7c22c5
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions includes/class-terraso-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit d7c22c5

Please sign in to comment.