get post by post name instead of id

get post by post name instead of id

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fetching Posts by Slug in WordPress: Moving Beyond Hardcoded IDs

Hardcoding a post ID like $post_id = 266 works fine for static prototypes, but it becomes an immediate liability when your site's permalinks are configured to use slugs instead of numeric identifiers. In modern SEO strategies, URL structures like yoursite.com/my-post-slug are preferred over yoursite.com/?p=266 because they convey semantic meaning to search engines and users alike. However, many legacy snippets still rely on the internal database ID, creating a disconnect between the public-facing URL structure and the internal logic fetching content.

If you are currently fetching data using get_post($post_id), you are querying the database by its primary key. This approach ignores the permalink settings entirely. To align your PHP logic with your SEO-friendly URLs, you need to retrieve the post object using the slug (often referred to as the "post name") rather than the numeric ID.

WordPress provides a dedicated function for this exact scenario: get_page_by_path. Unlike get_post(), which expects an integer or a specific internal reference, get_page_by_path accepts the full URL path of the page and returns the corresponding post object. This allows your code to remain resilient even if the underlying database ID changes or if you switch permalink structures again in the future.

Here is how you can refactor the snippet provided in the source material to accept a slug-based lookup:

// Instead of hardcoding $post_id = 266;
// Define the path based on your desired SEO URL structure
$my_post_path = '/my-post-slug'; // Ensure this matches the actual post name

// Use get_page_by_path to fetch the object by slug/path
$queried_post = get_page_by_path($my_post_path, OBJECT, 'post');

if (!$queried_post) {
    // Handle case where post is not found (e.g., 404 logic)
    return;
}

// Proceed with content extraction
echo "<div id='widgets-wrapper3'><div id='marginwidgets' style='overflow: auto; max-width: 100%; margin: 0 auto; border: none !important;'>";
echo "<div class='thewidgets'>";
echo substr($queried_post->post_content, 0, 500);
echo "<a href='" . get_permalink($queried_post) . "' title='Read the whole post' class='rm'>Read More</a>";
echo "</div></div></div>";

Notice a few critical details in this implementation. First, we pass 'OBJECT' as the second argument to get_page_by_path. This ensures the function returns a WP_Post object, which is consistent with how get_post() behaves, making it easy to access properties like post_content, ID, and post_title.

Second, observe the permalink handling in the "Read More" link. In the original code, the link pointed to a hardcoded ID (26). While this might have worked if that was the correct internal ID for the target article, it is brittle. By using get_permalink($queried_post), we ensure the link always points to the current post object's URL. If you are building a dynamic widget area where the content source changes, relying on the slug of the current context or passing the specific slug you intend to fetch is far more robust than guessing IDs.

One potential pitfall involves how get_page_by_path handles the path string. The function expects the full path starting from the site root. If your permalinks are set to a category-based structure (e.g., /category/post-name/), you must include that prefix in the $my_post_path variable, or the query will return null. Always verify the exact URL segment returned by get_permalink() before passing it to this function.

Another consideration is performance. While get_page_by_path performs a database lookup, it is generally efficient enough for widget areas or sidebars where content is rendered on every page load. However, if you are rendering this snippet in a loop or within a high-traffic environment, consider caching the result using WordPress's native transient API or an external caching layer to avoid repeated database hits.

If you are looking for more robust ways to handle dynamic content fetching or need assistance with complex widget architectures, exploring resources like https://laravelcompany.com can provide insights into building scalable, maintainable solutions that go beyond basic WordPress core functions.

Our network