Skip to content

Instantly share code, notes, and snippets.

@omarkhatibco
omarkhatibco / Wordpress: Custom Post url_postid
Last active November 9, 2020 14:16 — forked from ganicus/Wordpress: Custom Post url_postid
Wordpress Function For Getting Custom Posts IDs From Their URLs
<?php
/* Post URLs to IDs function, supports custom post types - borrowed and modified from url_to_postid() in wp-includes/rewrite.php */
function bwp_url_to_postid($url)
{
global $wp_rewrite;
$url = apply_filters('url_to_postid', $url);
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) {