0

I have a custom comments loop on my author.php page. I am trying to get the post id so that I can echo the permalink for the post that the comment belongs to. Also so I can echo some post meta custom fields.

This is what my loop looks like now -

<?php
$object = get_queried_object();
$authorID = get_queried_object()->ID;
$author_email = get_the_author_meta( 'user_email', $authorID );
$postid = get_queried_object()->post->ID;
$args = array(
    'user_id' => $authorID,
    'post_id' => $postid,
); 

// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

// Comment Loop
if ( $comments ) {
    foreach ( $comments as $comment ) { ?>

I tried place a normal post loop within this but things got weird. The part I need the post meta for is this --

<div class="full-divs">
    <strong>
        <a href="<?php echo get_permalink(); ?>">
            View 
            <?php 
                $property_address = get_post_meta( 
                    get_the_ID(),
                    'imic_property_site_address',
                    true
                ); 
                echo $property_address; 
            ?>
         </a>
     </strong>
 </div>

What am I doing wrong?

2

Note that it might be easier to use get_comments() here, since it's defined as:

function get_comments( $args = '' ) {
    $query = new WP_Comment_Query;
    return $query->query( $args );
}

If you need the comment url, within your foreach comment loop, you can use:

$comment_url =  esc_url( get_comment_link( $comment ) );

You can also get the post id from the $comment object with:

$post_id = $comment->comment_post_ID;

then you can use that to retrieve the custom post meta:

$property_address = get_post_meta( $post_id ,'imic_property_site_address',true);

To get the corresponding post permalink

$post_url = esc_url( get_permalink( $post_id ) );
|improve this answer|||||
  • I couldn't comment last night due to the phone issue, but I think you understood the question perfectly. ;-) – Pieter Goosen Nov 23 '15 at 4:33
  • If Im using my existing loop do I use it like this ` $object = get_queried_object(); $authorID = get_queried_object()->ID; $author_email = get_the_author_meta( 'user_email', $authorID ); $post_id = $comment->comment_post_ID; $args = array( 'user_id' => $authorID, );` cause right now its not doing anything when using $post_id for the permalink and custom field – Rich Nov 23 '15 at 6:02
  • if you want to get the post ID from each comment in the foreach loop, then you should place $post_id = $comment->comment_post_ID; within the foreach loop. @mygm26 – birgire Nov 23 '15 at 8:20
1

There is no $post property in get_queried_object(). get_queried_object() returns info about the current author when viewing the author archive page. You can do a var_dump() to check what get_queried_object() return

var_dump( get_queried_object() );

This is why your code fails. I am really not sure what you need to achieve here, but any post info can be accessed by using the main query object

  • $wp_query->posts returns an array of post objects. This is the posts that will be displayed by the loop

  • $wp_query->post returns the first post object of the loop, the same post object as $post before the loop

I probably think this is what you are after

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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