Timeline for Is it possible to get a page link from its slug?
Current License: CC BY-SA 2.5
6 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 23, 2018 at 3:53 | comment | added | mopsyd | This is a good answer. This bypasses the possiblity of a rogue plugin masking your page or incorrectly filtering it. If you return the id from the post table, then you can create an instance of \WP_Post from it, and that resolves directly in all of the wordpress functions that check for other values. \WP_Post also provides methods directly to find most related data about the post. | |
| May 13, 2013 at 11:08 | comment | added | Matheus Eduardo | @webcitron I think just because is following original pattern of Wordpress getting post by 'title', just changing for 'slug'. (check the link) | |
| Jan 9, 2013 at 14:53 | comment | added | s_ha_dum | Why? It doesn't generate a complete post object just to get the ID. | |
| Jan 9, 2013 at 14:27 | comment | added | user25790 | Last comment - I think that sql needs to have one more condition: 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 AND post_status = 'publish'", $page_slug, $post_type ) ); if ( $page ) return get_page($page, $output); return null; } | |
| Apr 11, 2012 at 13:11 | comment | added | julien_c | Why would that be better? Can you explain? | |
| Feb 28, 2011 at 15:21 | history | answered | Matheus Eduardo | CC BY-SA 2.5 |