Created
August 13, 2025 18:19
-
-
Save kpirnie/b0bffebce34d5bfac0d06b400ceb4a2e to your computer and use it in GitHub Desktop.
WordPress - Get All Publilc Post Type URLs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * get_urls | |
| * | |
| * Public method pull to gather all public URLs | |
| * | |
| * @since 7.3 | |
| * @access public | |
| * @author Kevin Pirnie <[email protected]> | |
| * @package Kevin's Framework | |
| * | |
| * @return array Returns an array of URLs | |
| * | |
| */ | |
| public function get_urls( ) : array { | |
| // see if we've already got this in cache | |
| $_urls = wp_cache_get( "kfw_urls", "kfw_urls" ); | |
| // if we have it in our cache already | |
| if( $_urls ) { | |
| // return it | |
| return $_urls; | |
| // we don't | |
| } else { | |
| // hold our return array | |
| $_ret = array( ); | |
| // all we're interested in is publicly accessible posts/pages | |
| $_post_types = get_post_types( array( 'public' => true, ), 'names' ); | |
| // setup some arguments | |
| $_args = array( | |
| 'no_found_rows' => true, | |
| 'update_post_meta_cache' => false, | |
| 'update_post_term_cache' => false, | |
| 'fields' => 'ids', | |
| 'post_type' => $_post_types, | |
| 'posts_per_page' => -1, | |
| 'post_status' => 'publish', | |
| ); | |
| // our query for all public posts | |
| $_qry = new WP_Query( $_args ); | |
| // fire up the recordset | |
| $_rs = $_qry -> get_posts( ); | |
| // make sure we have a recordset | |
| if( $_rs ) { | |
| // loop and and populate the URL array with the posts permalinks | |
| foreach( $_rs as $_id ) { | |
| // add it to the return array | |
| $_ret[] = get_permalink( $_id ); | |
| } | |
| } | |
| // cache it for 1 hour | |
| wp_cache_add( "kfw_urls", $_ret, "kfw_urls", HOUR_IN_SECONDS ); | |
| // return the array | |
| return $_ret; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment