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;
}

Tuesday, December 10, 2013

in_array function in javascript

This is a simple post for the converted in_array function in javascript. This function will return boolean whether the value is in the array or not.

function in_array(array, id) {
    for(var i=0; i<array.length; i++) {
        if (array[i]==id) return true;
    }
    return false;
}



Hope you like this post, probably short but will help a lot.

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.