I recently had a case where I needed to get the post object for a custom post type in WordPress – I had to do this by a slug that the user passed in. There is a function ( get_page_by_path) which I initially thought was to only find the post object for pages but it actually goes deeper.

The function takes 3 arguments:

  1. The path/slug that you’re looking for.
  2. How you want the post to be returned – basically as an array or object.
  3. The post type(s) you want to search through.

So to get the post object (let’s call my custom post type ‘book’) I can just do the following:

Now that I have the object I can reference it as I would any post object in WordPress:

For more information, you can visit the get_page_by_path function reference on WordPress.

Published by Seagyn

I'm a passionate "Maker of Things" and build as often as I can. I build things with mainly with PHP and a little Javascript. View more posts

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

',a='
';return t.replace("ID",e)+a}function lazyLoadYoutubeIframe(){var e=document.createElement("iframe"),t="https://www.youtube.com/embed/ID?autoplay=1";t+=0===this.dataset.query.length?'':'&'+this.dataset.query;e.setAttribute("src",t.replace("ID",this.dataset.id)),e.setAttribute("frameborder","0"),e.setAttribute("allowfullscreen","1"),this.parentNode.replaceChild(e,this)}document.addEventListener("DOMContentLoaded",function(){var e,t,a=document.getElementsByClassName("rll-youtube-player");for(t=0;t