There 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, "https://wordpress.stackexchange.com/" ); // Trim slashes
$posts_query = new WP_Query( array(
'name' => $slug
) );
if ( $posts_query->have_posts() ) {
$postid = $posts_query->posts[0]->ID;
}
Related Posts:
- WP_Query vs get_posts
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- WP_Query: query posts by ids from array?
- pre_get_posts with get_posts
- Query Custom Meta Value with Increment
- Identify which loop you are hooking into; primary or secondary?
- Transient pagination not working properly
- meta_query on a date range using an array of values
- Get the post permalink within the loop but without additional DB query
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- Undefined WP_Query::has_posts()?
- Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX
- if/else on custom query gives 200 OK when condition not met?
- Order by empty custom field
- Check if loop has any categories?
- Is_single() conditional tag returns null in query
- Transient api Caches confused
- Include current post into loop
- Should I reset $wp_query?
- How to filter query loop block with a search string from the query parameters
- Loop posts based on permalink term
- I am officially missing something about transient posts
- WP_Query & Duplicate entries
- how to show more than 1 post into three columns query
- Ordering Posts by parent category, name ascending
- WP_Query secondary query failing
- Loop through multiple custom fields with increasing number
- meta_query dates from an array
- Main loop querying current template’s info only in custom category archive pages, not my posts
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Multiple queries and pagination
- Change query from cat id to slug or name?
- How do I get my custom query to work with search results after the first page?
- Query only the posts with a post format of “audio”
- Modify Search Query if original Query gave no results
- Using Wp_Query without the loop?
- 2 wordpress loops showing 1 post from same post type – how to avoid showing the same post?
- How do I rewrite this loop as a new WP_Query style-loop?
- Issue with front page navigation after upgrading to 3.4
- How can I override one post and make it display content for another post?
- Custom WP_Query id
- Best choice for multiple loop in page?
- Is it possible to query from external database? [duplicate]
- get_template_part causes 500 error
- WP Query – order posts by meta field first and then order the rest
- Can’t sort custom post type by random
- pre_get_posts – Trying to get property of non-object warning
- Query posts that contain video?
- Get multiple users with meta value in one query and populate WP_User class
- WordPress query posts with multiple post_meta data
- How to display the featured image for each post?
- Assign custom parameter to each post in query
- How to provide meta_key array to wp_query?
- Wp_query with 2 meta keys and array of meta values
- Adding a html class based on post count
- Check for the main query from the template
- how to use pre_gets_posts to exclude one queried ID from homepage loop
- How to add div blocks after certain set of post
- Trying to retrieve random post, getting a page
- How to overwrite orderby with add_query_var
- Filter post query to only show direct children of category
- Posts in loop all show the same author when there are many authors
- Exclude recently updated post from custom WP_Query using multiple loops
- Get a list of posts by specific category
- get_posts output always same post
- Show a 404 error page if Public query variable’s value doesn’t exist?
- Exclude taxonomy term from all loops, but having it on widget
- Multiple Rows in a using wp_query
- How to use WP_Query() on single.php?
- Default permalink structure causing Notice: Undefined property: WP_Query::$post
- How to check the array values, what WP_Query has brought to me?
- sort and display posts by custom field (wp-query and the loop))
- How to create custom query by keyword in post title?
- difference between querying database and using the loop
- If have posts a week old, display, else display a different loop
- WP_Query – Adding “offset” posts to the end of the loop
- Stomping WP_Query in author archive to facilitate pagination with custom queries
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Conditional operator OR not working with custom fields
- How to set up hierarchical relationships without using plugins / meta query
- How to add custom meta to ‘pre_get_terms’?
- Set front_page programatically after user login via query, while leaving site option alone
- Loop increase in while loop not working
- Create a loop on my pages with new “WP_Query”
- Get posts that do not have the same tags as current
- posts archive page – closing WP_Query loop correctly [closed]
- Create a page template for “top rated posts” but show full content and not just a list
- has_excerpt() not working inside wp_query
- Shortcode for custom query not returning results when attributes are added
- wp_query is showing posts from other categories
- Daily drip of posts – based on user ‘startData’ – ordered oldest to newest
- How to add tax_query to $args with concatenation
- Meta query compare for ID’s greater than specific ID
- Exclude a Woocommerce product from WP_Query
- WP ForLoop to compare meta information of posts to determine what post to display
- Yoast primary category query modification
- Query where ANDing slug values not working
- AJAX Breaking Offset Argument In WP Query
- WP_QUERY wrong ammount of posts
- How to add sort order to incremented and paginated category loop