Tuesday, December 17, 2013

get_page_by_slug in wordpress

I'm developing a wordpress site and I need a function that will retrieve the page information from a given slug. I'd been looking into wordpress functions but I can't find it, what I saw are just get_page_by_path and get_page_by_title which are not the perfect function I need for the functionality I need for the site.

So, here's what I did. I checked out the existing functionality which is most likely the same of what I wanted to achieve get_page_by_title then from there I was able to arrived a new function get_page_by_slug that will retrieve the page information from a slug.

Please see below, hope this helps a lot on building your wordpress site and hopefully this would be included on the next version of wordpress.

/**
 * Retrieve a page given its slug.
 * @uses $wpdb
 *
 * @param string $page_slug Page slug
 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
 * @param string $post_type Optional. Post type. Default page.
 * @return WP_Post|null WP_Post on success or null on failure
 */
function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) {
    global $wpdb;
    $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $page_slug, $post_type ) );
    if ( $page )
        return get_post( $page, $output );

    return null;
}


Please take note that you use this as well on retrieving post coz technically, page and post are the same but with different post type, you just need to pass on third parameter as "post". To make it much clearer with names, please see get_post_by_slug function below.

/**
 * Retrieve a post given its slug.
 * @uses $wpdb
 *
 * @param string $slug Post slug
 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
 * @param string $type Optional. Post type. Default page.
 * @return WP_Post|null WP_Post on success or null on failure
 */
function get_post_by_slug($slug, $output = OBJECT, $type = 'post' ) {
    global $wpdb;
    $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $slug, $type ) );
    if ( $post )
        return get_post( $post, $output );

    return null;
}

10 comments:

  1. My site use this get_page_by_path('your-slug-pos');
    amaperfect.com

    ReplyDelete
  2. Customer service is the service that you provide your clients. Currently a customer or a client is someone who purchases a service or a product from your organization. Customer service is the very crucial part of a business and really basic. Just making someone feel client. Helping them solve any queries they worries or have, and only looking after their demands.
    https://servicecenterguru.com

    ReplyDelete
  3. A long time ago, when someone said WordPress, everyone automatically thought of blogging. While it is still the best solution to use for blogging, it has come a long way and has become a very comprehensive Content Management Suite used to power many different types of websites, including business sites, job boards, sites featuring classified ads, etc.https://medijo.lt/lankstinukai/

    ReplyDelete
  4. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. make a website

    ReplyDelete
  5. Once you have your hosting, and they've told you how to upload your files (FTP), then you should proceed to setting up a MySQL database, so WordPress can store all of your content.wordpress maintenance service

    ReplyDelete
  6. hi was just seeing if you minded a comment. i like your website and the thme you picked is super. I will be back. Themify review

    ReplyDelete
  7. The substance that you make is put away in a database neuronto deepl wordpress plugin

    ReplyDelete
  8. Incredible articles and awesome design. Your blog entry merits the greater part of the positive input it"s been getting. wordpress

    ReplyDelete

  9. Hello,

    we provide affordable and result-oriented SEO services, please give a chance to serve you.


    Thanks
    Admin: E07.net

    ReplyDelete
  10. You have outdone yourself this time. It is probably the best, most short step by step guide that I have ever seen. wordpress plugin

    ReplyDelete

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.