如何使用SQL获取永久链接

时间:2017-02-25 作者:Kallol Das

我正在尝试通过sql获取文章标题。我的sql代码是

$query ="SELECT     wp_posts.post_title AS title , 
                    wp_posts.post_content AS content,
                    wp_posts.post_date AS blogdate 
        FROM wp_posts
        WHERE wp_posts.post_status = \'publish\'
        ORDER BY wp_posts.post_date DESC ";
global $wpdb;
$result= $wpdb->get_results($query);


 if ($result){

    foreach($result as $post){
    ?><li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    <?php echo $post->title; ?></a></li><?php
    }
 }else{
     echo "Sorry, No Post Found.";
 }

    die();
现在的问题是我得到了标题,但\\u permalink()不起作用。

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

我不知道您为什么使用自定义查询get_posts()WP_Query 这是你应该做的。

无论如何,你需要获取帖子ID…

$query ="SELECT     wp_posts.post_title AS title , 
                    wp_posts.post_content AS content,
                    wp_posts.post_date AS blogdate ,
                    wp_posts.ID AS ID
然后把这个ID传给get_permalink():

get_permalink( $post->ID );
标题属性类似:

the_title_attribute( [ \'post\' => $post->ID ] );

SO网友:DavidGM

Let\'s see...

select p.post_title Title, p.post_date TimeStamp, (SELECT MIN(meta_value) FROM wp_postmeta WHERE meta_key=\'PermaLink\' AND post_id = p.ID) AS PermaLink 
  from wp_terms
  inner join wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
  inner join wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
  inner join wp_posts p on p.ID = wpr.object_id 
  /* addidtional conditions - update for your needs */
   where taxonomy= \'category\' and p.post_type = \'post\' and wp_terms.name = \'News\' and p.post_status = \'publish\' 
   order by post_date DESC;

相关推荐

get_posts custom field

这是一个愚蠢的问题,但我找不到合适的方式问谷歌。所以,如果这是一个重复的问题,很抱歉。我提供了一个带有复选框的自定义字段,用户可以检查是否希望此特定帖子进入主页。因此,在我的主页上,我呼吁所有已激活选中的帖子。我正在为自定义帖子类型CV创建自定义字段:function add_custom_post_meta_box() { add_meta_box( \'custom_post_meta_box\', // $id \'Campos Per