Skip to content

Commit 8d1e1d3

Browse files
authored
Merge pull request #94 from emgk/issue/4382
Added support for post_title for command:post generate
2 parents 0dfecf2 + 66179ee commit 8d1e1d3

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

features/post-generate.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,21 @@ Feature: Generate new WordPress posts
4242
Success:
4343
"""
4444

45+
Scenario: Generating post and outputting title and name
46+
When I run `wp post generate --count=3 --post_title=Howdy!`
47+
And I run `wp post list --field=post_title --posts_per_page=3`
48+
Then STDOUT should contain:
49+
"""
50+
Howdy!
51+
Howdy! 2
52+
Howdy! 3
53+
"""
54+
And STDERR should be empty
55+
And I run `wp post list --field=post_name --posts_per_page=3`
56+
Then STDOUT should contain:
57+
"""
58+
howdy
59+
howdy-2
60+
howdy-3
61+
"""
62+
And STDERR should be empty

src/Post_Command.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ public function list_( $_, $assoc_args ) {
418418
* default: publish
419419
* ---
420420
*
421+
* [--post_title=<post_title>]
422+
* : The post title.
423+
* ---
424+
* default:
425+
* ---
426+
*
421427
* [--post_author=<login>]
422428
* : The author of the generated posts.
423429
* ---
@@ -475,6 +481,7 @@ public function generate( $args, $assoc_args ) {
475481
'post_author' => false,
476482
'post_date' => current_time( 'mysql' ),
477483
'post_content' => '',
484+
'post_title' => '',
478485
);
479486
extract( array_merge( $defaults, $assoc_args ), EXTR_SKIP );
480487

@@ -495,7 +502,7 @@ public function generate( $args, $assoc_args ) {
495502
// Get the total number of posts.
496503
$total = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s", $post_type ) );
497504

498-
$label = get_post_type_object( $post_type )->labels->singular_name;
505+
$label = ! empty( $post_title ) ? $post_title : get_post_type_object( $post_type )->labels->singular_name;
499506

500507
$hierarchical = get_post_type_object( $post_type )->hierarchical;
501508

@@ -531,11 +538,11 @@ public function generate( $args, $assoc_args ) {
531538

532539
$args = array(
533540
'post_type' => $post_type,
534-
'post_title' => "$label $i",
541+
'post_title' => ! empty( $post_title ) && $i === $total ? "$label" : "$label $i",
535542
'post_status' => $post_status,
536543
'post_author' => $post_author,
537544
'post_parent' => $current_parent,
538-
'post_name' => "post-$i",
545+
'post_name' => ! empty( $post_title ) ? sanitize_title( $post_title . ( $i === $total ) ? '' : '-$i' ) : "post-$i",
539546
'post_date' => $post_date,
540547
'post_content' => $post_content,
541548
);

0 commit comments

Comments
 (0)