Wordpress: Get GUID from page title


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.