admin管理员组文章数量:1122846
I am using an API which tracks metrics on parts of my site. The only useful bit it saves though is the URL (permalink).
What is the most efficient way to query up all the posts that match those permalinks, given the fact that I don't have access to the ID's to use post__in
with WP_Query
.
I am using an API which tracks metrics on parts of my site. The only useful bit it saves though is the URL (permalink).
What is the most efficient way to query up all the posts that match those permalinks, given the fact that I don't have access to the ID's to use post__in
with WP_Query
.
1 Answer
Reset to default 0There are several ways to achieve this, depending on the permalink structure of your posts. Assuming the post slug is part of the permalink structure, you can get the post slug from the post URL and fetch the corresponding post by using WP_Query
with the name
-parameter. Let's assume the structure of the URLs is http://example.com/{post_slug}/
. We can fetch the slug by using parse_url and query the posts with that slug:
$url = 'http://example.com/my-post/';
$path = parse_url( $url, PHP_URL_PATH ); // Get URL path from URL
$slug = trim( $path, '/' ); // Trim slashes
$posts_query = new WP_Query( array(
'name' => $slug
) );
if ( $posts_query->have_posts() ) {
$postid = $posts_query->posts[0]->ID;
}
本文标签: wp queryHow to get list of posts from permalinks
版权声明:本文标题:wp query - How to get list of posts from permalinks? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288137a1928031.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
name
parameter. – Abhik Commented May 30, 2014 at 5:39['hello-world', 'post-2', 'welcome-post']
, I'm not sure what you mean – Tallboy Commented May 30, 2014 at 15:14