2010-08-12 50 views
9

尋找一種方法來導出wordpress中漂亮的固定鏈接列表以及相應的帖子標題。尋找實際的永久鏈接結構而不是短鏈接。我想如果我必須使用簡短的鏈接,但我更喜歡完整的固定鏈接。漂亮的固定鏈接和帖子標題的導出列表

任何建議將有所幫助。

感謝

+0

幫助我們知道所涉及的表格和列 - 我們並不都知道Wordpress數據模型 – 2010-08-12 04:12:05

+1

@OMG小馬:如果您不知道WordPress數據模型,您將無法幫助回答這個問題題;永久鏈接不直接存儲在數據庫中。謝天謝地,我們都會有一個新的來自StackExchange的WordPress解答在本週內上市!當發生這種情況時,向那些詢問WordPress問題的人員發佈路由可能是有道理的,因爲我們將擁有大量熟悉WordPress數據模型以及WordPress所有其他方面的用戶。 – MikeSchinkel 2010-08-14 02:50:08

+0

@MikeSchinkel:* Riight * ...因爲我從未*不得不在學習別人的數據模型之前提供查詢優化反饋。如果問題很普遍,我已經瞭解了數據模型。但祝你好運! – 2010-08-14 03:01:09

回答

29

這裏是一個獨立的PHP文件可以保存到你的網站的根稱爲像/export.php,當你與你的瀏覽器中調用它,它就會發出帖子的製表符分隔的純文本清單,漂亮的固定鏈接,帖子標題和(作爲獎勵)帖子類型。

只需在瀏覽器中加載該URL,然後將「另存爲」添加到文本文件中,然後您可以在Excel中加載該文件,否則需要對其進行處理。

<?php 

include "wp-load.php"; 

$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish'); 
$posts = $posts->posts; 
/* 
global $wpdb; 
$posts = $wpdb->get_results(" 
    SELECT ID,post_type,post_title 
    FROM {$wpdb->posts} 
    WHERE post_status<>'auto-draft' AND post_type NOT IN ('revision','nav_menu_item') 
"); 
*/ 

header('Content-type:text/plain'); 
foreach($posts as $post) { 
    switch ($post->post_type) { 
     case 'revision': 
     case 'nav_menu_item': 
      break; 
     case 'page': 
      $permalink = get_page_link($post->ID); 
      break; 
     case 'post': 
      $permalink = get_permalink($post->ID); 
      break; 
     case 'attachment': 
      $permalink = get_attachment_link($post->ID); 
      break; 
     default: 
      $permalink = get_post_permalink($post->ID); 
      break; 
    } 
    echo "\n{$post->post_type}\t{$permalink}\t{$post->post_title}"; 
} 

希望這會有所幫助。

-Mike

P.S.我使用標準WordPress WP_Query(),但也包括一個註釋掉的SQL,以防您更喜歡(或需要)使用它。

+0

感謝MIke的幫助和理解。我得到一個警告:由於某種原因在第7行爲foreach()提供的無效參數 – jeff 2010-08-14 13:41:14

+0

對不起,我在發佈代碼之前做了一個不幸的編輯,doh!固定! – MikeSchinkel 2010-08-15 18:51:13

+0

謝謝邁克, 舊帖子,但仍然非常有效的片段,只是做了把戲;-) 太棒了! – quokka 2013-06-21 19:29:00

6

今天上午:) http://wp.daveheavyindustries.com/2011/02/08/wordpress-permalink-via-sql/

此查詢應該爲你

SELECT wpp.post_title, 
     wpp.guid, 
     wpp.post_date, 
     CONCAT 
     (
      wpo_su.option_value, 
      REPLACE 
      (
      REPLACE 
      (
       REPLACE 
       (
       REPLACE 
       (
        wpo.option_value, 
        '%year%', 
        date_format(wpp.post_date,'%Y') 
       ), 
       '%monthnum%', 
       date_format(wpp.post_date, '%m') 
      ), 
       '%day%', 
       date_format(wpp.post_date, '%d') 
      ), 
      '%postname%', 
      wpp.post_name 
     ) 
     ) AS permalink 
    FROM wp_posts wpp 
    JOIN wp_options wpo 
    ON wpo.option_name = 'permalink_structure' 
    AND wpo.blog_id = 0 
    JOIN wp_options wpo_su 
    ON wpo_su.option_name = 'siteurl' 
    AND wpo_su.blog_id = wpo.blog_id 
WHERE wpp.post_type = 'post' 
    AND wpp.post_status = 'publish' 
ORDER BY wpp.post_date DESC 
0

我也希望這個解決方案,並感謝@MikeSchinkle爲原來的解決方案做回答這一項上EE。我確實使用它將這些鏈接以純文本格式導出爲Excel,然後構建我的重定向列表。

但後來我發現我也想要一個帶有活動鏈接的解決方案。

所以我使用wp_query使用帖子類型「任何」,並創建了一個page template與下面的查詢(包括自定義以適合您的主題,你認爲合適)的搜索表單。注意我必須將posts_per_page設置爲-1才能返回無限的結果。這返回結果爲:「標題 - 永久鏈接」

<?php 
     $type = 'any'; 
     $args = array (
     'post_type' => $type, 
     'post_status' => 'publish', 
     'posts_per_page' => -1, 
      'order' => 'DESC', 

     ); 
     $temp = $wp_query; // assign ordinal query to temp variable for later use 
     $wp_query = null; 
     $wp_query = new WP_Query($args); 
     if ($wp_query->have_posts()) : 
      while ($wp_query->have_posts()) : $wp_query->the_post(); 
      ?> 

       <?php the_title(); ?> - <a href="<?php the_permalink() ?>" rel="bookmark" title="View The <?php the_title_attribute(); ?>"><?php the_permalink() ?></a><br /> 
<?php endwhile; ?> 
<?php else : 
      echo '<h2>Sorry, we didnt find any results to match. Please search again below or call us at 800-828-4228 and we will be happy to help!</h2>'; 
      get_search_form(); 
     endif; 

    $wp_query = null; 
    $wp_query = $temp; // Reset 
?> 

希望能幫助別人。