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

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


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" ) );

  • 2
    Upvoting for the extra effort taken to create the expanded view - something we don't see here, but I think that formatting can help in a lot of these Q&A's. – TonyG Feb 20 '18 at 1:04
  • This didn't work for me. Ran it but got a... ERROR 1146 (42S02) at line 1: Table 'mysite-wp-OPSRFxQc.wp_posts' doesn't exist – Jay Mar 28 '18 at 22:15
  • @Jay Maybe your table prefix is different from wp_ ? – birgire Mar 28 '18 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.