Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i am trying to display comments by specific users at a custom area. i am successful to do that, but the problem is the permalink structure . this code gets the url as permalinks disabled, if i enable the permalinks from my settings , then these urls starts giving a 404.

here is the code im using :

<?
if(get_query_var('author_name')) :
$curauth = get_userdatabylogin(get_query_var('author_name'));
else :
$curauth = get_userdata(get_query_var('author'));
endif;
$querystr = "
    SELECT comment_ID, comment_post_ID, post_title, comment_content
    FROM $wpdb->comments, $wpdb->posts
    WHERE user_id = $uid
    AND comment_post_id = ID
    AND comment_approved = 1
    ORDER BY comment_ID DESC LIMIT 5
 ";
 $comments_array = $wpdb->get_results($querystr, OBJECT);
if ($comments_array): ?>
   <ul>
<? foreach ($comments_array as $comment):
    setup_postdata($comment);
    echo "<li><a href='". get_bloginfo('url') ."/?p=".$comment->comment_ID."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
endforeach; ?>
</ul>
<? endif; ?>

So this gets the url as www.mysite.com/p?=xxxx .. if permalinks are enabled as mysite.com/post-perma-links/ it starts giving 404 on these links generated via the above code. rest of the function is workign fine,

SO possible to get the permalink url in this case instead of the p?=xxx ? help is appreciated cheers

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Hi @Ayaz Malik:

You need to use the function get_comment_link(). I've rewritten your code using some improved techniques and included the function call in place of what you had:

global $wpdb;
$sql =<<<SQL
SELECT
  {$wpdb->comments}.comment_ID,
  {$wpdb->comments}.comment_post_ID,
  {$wpdb->comments}.comment_content,
  {$wpdb->posts}.post_title
FROM
  {$wpdb->comments}
  INNER JOIN {$wpdb->posts} 
    ON {$wpdb->comments}.comment_post_id={$wpdb->posts}.ID
WHERE 1=1
  AND {$wpdb->comments}.user_id = %d
  AND {$wpdb->comments}.comment_approved = 1
ORDER BY
  {$wpdb->comments}.comment_ID DESC
LIMIT 5
SQL;
$sql = $wpdb->prepare($sql,$uid);  // $uid is assumed pre-defined before this code
$comments = $wpdb->get_results($sql, OBJECT);
if ($comments) {
  echo '<ul>';
  foreach ($comments as $comment) {
    setup_postdata($comment);
    $link = get_comment_link($comment->comment_ID);
    echo "<li><a href='{$link}'>Comment on {$comment->post_title}</a><br />" .
           "{$comment->comment_content}</li>";
  }
  echo '</ul>';
}
share|improve this answer
    
Bravo my friend ! Hit in first try , thanks a lot :) –  Ayaz Malik Jan 30 '11 at 12:38
    
@Ayaz Malik - I'm rarely good, but when I am I'm really good. :) Glad I could help. –  MikeSchinkel Jan 31 '11 at 2:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.