$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp cli - Using wp-cli can I not query pages by their title?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp cli - Using wp-cli can I not query pages by their title?

matteradmin8PV0评论

All of these do the same thing:

wp post list --post_type=page
wp post list --post_type=page --post_title=dfdsfds
wp post list --post_type=page --title=sdfdsf

How can I query the post by the title? By contrast, using --name it works fine:

wp post list --post_type=page --name=sdfdsf

All of these do the same thing:

wp post list --post_type=page
wp post list --post_type=page --post_title=dfdsfds
wp post list --post_type=page --title=sdfdsf

How can I query the post by the title? By contrast, using --name it works fine:

wp post list --post_type=page --name=sdfdsf
Share Improve this question asked Aug 25, 2015 at 19:13 Kit SundeKit Sunde 3711 gold badge4 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 9

If we restrict us to the output of wp post list, then here's a way to search for %test% within the post titles of published posts:

wp post list --ignore_sticky_posts=1 --post__in=$(wp db query 'SELECT ID FROM wp_posts WHERE post_title LIKE "%test%" AND post_status="publish" AND post_type="post"' --skip-column-names |  paste -s -d ',' - )

Here's the expanded view:

wp post list                                 #<-- the first wp command
    --ignore_sticky_posts=1                  #<-- get rid of stickies
    --post__in=                              #<-- only specific post IDs
        $(wp db query                        #<-- the second wp command
            'SELECT ID                       #<-- only get the post IDs
             FROM wp_posts                   #<-- table name, adjust the prefix!
             WHERE post_title LIKE "%test%"  #<-- title search word
               AND post_status="publish"     #<-- published
               AND post_type="post"'         #<-- posts
          --skip-column-names                #<-- Skip the ID column name
          | paste -s -d ',' -                #<-- csv
        )

Update:

For exact cases, one can replace post_title LIKE "%test%" with post_title = "test".

We can use --skip-column-names to remove the ID column name from the inner query output, instead of using sed 's/^ID,//' to remove it. It would be nice if we could use --format=csv instead of using | paste -s -d ',' -, but currently that doesn't seem to be supported on the wp db query command.

There's also the wp db search command but currently we can only restrict it to a given table(s), like wp db search test wp_posts, but not a given table column. One could e.g. try to play with wp db search test wp_posts --one_line | grep wp_posts:post_title, i.e. search the whole wp_posts table and filter the output that contains wp_posts:post_title, thanks to --one_line. But note this is searching the whole table first!

There's also the wp shell that prompts the wp-cli shell, where one has access to the WordPress classes and functions, like WP_Query and get_page_by_title().

It's also possible to the run wp eval command and the wp evail-file command to run a given php code. Here's an example: wp eval 'print_r( get_page_by_title( "test" ) );

Post a comment

comment list (0)

  1. No comments so far