wordpress get all publish pages permalinks

I want to get the permalinks of all published pages and export it to an excel file.

What is the best solution?

  • Should I go to wp-admin panel and copy-paste the permalinks from the on Page editor?
  • Can I get the export data using a mysql query?

Related posts

1 comment

  1. It is possible but very difficult to do via a SQL query, because WordPress allows you to change your Permalink structure – so the SQL query would need to regard all permalink options to build the correct permalink when the query is executed.

    And I guess you are looking for a way that does not require you to copy paste links from the admin page 😉

    By far the best and easiest thing to do is write a small script that runs the export for you within WordPress and uses the functions get_pages and get_permalink:

    // Get a list of all published pages from WordPress.
    $pages = get_pages( 'post_status=publish' );
    
    // Loop through all pages and fetch the permalink for each page.
    foreach ( $pages as $page ) { //fixed this too
      $permalink = get_permalink( $page->ID );
      // Do something with the permalink now...
    }
    

    Note: This code will only run inside WordPress, i.e. as as Plugin or inside a Theme. Also how to do the excel export is beyond the scope of my answer…


    Personally I’d create a WordPress plugin (there are many guides out there on how this works, like this one).
    In the plugin you could simply check for a URL param, and if this param is present then export the data. Additionally I would check if the user that requests the export has admin permissions before exporting the data.

    A bit like this:

    /** 
     * The WordPress plugin header... 
     */
    
    $allowed = false;
    $requested = false;
    
    // Check if the current user has admin capabilies.
    if ( current_user_can( 'manage_options' ) ) { $allowed = true; }
    
    // Check if the user requested the export.
    // i.e. by calling URL http://yoursite.com?export_permalinks=1
    if ( 1 == $_GET['export_permalinks'] ) { $requested = true; }
    
    if ( $requested && $allowed ) {
      // 1. Get a list of all published pages from WordPress.
      $pages = get_pages( 'post_status=publish' );
    
      // 2. Build the export data.
      $export = array();
      foreach ( $pages as $page ) {
        $permalink = get_permalink( $page->ID );
        $export[] = array( $page->ID, $permalink );
      }
    
       // 3. Export the data (I just var_dump it as example).
       var_dump( $export );
    
       // We're done, so don't execute anything else.
       exit;
    }
    

    Please note that this code should only explain my suggested workflow. It does not use best practices and I don’t recommend to use it like this on a live site

Comments are closed.