Posted By

prwhitehead on 04/17/11


Tagged

post wordpress title guid posttitle


Versions (?)

Wordpress: Get GUID from page title


 / Published in: PHP
 

Dynamically get the guid of your a post from its post title (with wordpress caching)

  1. /**
  2. * Usage: <a href="<? echo get_required_page('Sitemap'); ?>" title="Sitemap">Sitemap</a>
  3. *
  4. *
  5. */
  6.  
  7. function get_required_page($page = ''){
  8. global $wpdb;
  9.  
  10. $result = wp_cache_get($page . '-guid', __FUNCTION__);
  11.  
  12. if($result === false)
  13. {
  14. $result = $wpdb->get_var("SELECT p.guid
  15. FROM $wpdb->posts p
  16. WHERE p.post_status = 'publish'
  17. AND p.post_title = '{$page}' ");
  18.  
  19. if ($result)
  20. {
  21. wp_cache_add($page . '-guid', $result, __FUNCTION__);
  22. }
  23. }
  24. return $result;
  25. }

Report this snippet  

You need to login to post a comment.