I use
get_page_by_title()
sometimes in my code, for example
get_page_by_title($article['title'], OBJECT, 'post');
but now documentation says i cant use it anymore. It was pretty convenient for the purpose of testing some features where I can’t use ids because of creating content on different databeses for testing purposes.
Is there any way except using WP_Query
class because its look like a pain to use it just for getting link by the title of the post
I am looking for something like
get_url_by_title('title', 'post')
Any thoughts?
3
Answers
As mentioned @CBroe in comments, the best substitution for deprecated
get_page_by_title()
isget_posts()
documentation. Ok, I know that documentation says: "Use WP_Query instead.", but it looks like pain.So back to solution. To get a single page or post by title, use
There are some moments.
'post_type' => 'any'
works better than particular post type. Do not forget thatget_posts()
returns an array of objects, so you need to refer to an array and then to object.But if you need to get a link for a single category, you can use
You can try this:
For more detail please visit the reference.
Well, since the reason to deprecate this function is stupid [corporate non-sense ala Automattic https://core.trac.wordpress.org/ticket/57041 ]
You can just un-deprecate it! Use this function instead: