Skip Navigation

[Resolved] Cannot query another post according to current post author

This thread is resolved. Here is a description of the problem and solution.

Problem:

I have a custom post "car" and a content template for this post. I am using custom shortcode to query another custom post according to the post author, it does not work, see details here:

https://toolset.com/forums/topic/cannot-query-another-post-according-to-current-post-author/#post-1232094

Solution:

It is a custom codes problem, see the solution here:

https://toolset.com/forums/topic/cannot-query-another-post-according-to-current-post-author/#post-1232113

Relevant Documentation:

https://developer.wordpress.org/reference/functions/get_permalink/

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by kelvinL-2 5 years ago.

Assisted by: Luo Yang.

Author
Posts
#1232094
screencapture-hicar-hk-car-c8253-2019-04-17-16_59_58.png
screencapture-hicar-hk-car-c8253-2019-04-17-17_00_27.png
Screenshot 2019-04-17 at 5.04.50 PM.png

Dear Sir/Madam,

I have a custom post "car" and a content template for this post. I have below shortcode to query another custom post according to the post author

function get_dealer_url( $atts) {
    $args = array( 'posts_per_page' => 1, 'post_type'=> 'dealer', 'author_name' => $atts['author'] );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) {
        setup_postdata($post);
        return get_permalink();
    }
}
add_shortcode('get_dealer_url', 'get_dealer_url');

I pass the wpv-post-author to query details from another custom post "dealer".

<tr><td class="d-label">車行主頁:</td><td><span class="trimtext"><a href="[get_dealer_url author='[wpv-post-author]']">[get_dealer_url author='[wpv-post-author]']</a></span></td></tr>

Don't know why it always returns the current car post url, instead of the dealer post url.

If I add the global $post in function, most of the car post details lost.

Best regards,

Kelvin

#1232113

Dear Kelvin,

You can replace these two lines from:

        setup_postdata($post);
        return get_permalink();

To:

        return get_permalink($post->ID);

See wordpress document:
https://developer.wordpress.org/reference/functions/get_permalink/
$post
(int|WP_Post) (Optional) Post ID or post object.

#1232130

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.