admin管理员组文章数量:1130349
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
1 Answer
Reset to default 9If 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" ) );
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
1 Answer
Reset to default 9If 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" ) );
本文标签: wp cliUsing wpcli can I not query pages by their title
版权声明:本文标题:wp cli - Using wp-cli can I not query pages by their title? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749078193a2312635.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论