Support » Fixing WordPress » get_permalink returns wrong URL

  • Resolved oxfordian3


    I’ve been hammering at this for a while, and can’t make it work.

    The WP site has been live for a while and works well. It has pages with a standard have_posts() loop to produce the current query’s posts in the main column, and then there’s a sidebar with various possible widgets, also doing some post loops.

    On a regular category page, it is generating the correct posts in the main Loop displayed in the main content area.

    Then in the sidebar, I use get_posts() to retrieve an unrelated set of posts to display. It loops through each post returned by get_posts, retrieves the ID of the post, then displays the Title and Permalink with get_the_title(ID) and get_permalink(ID).

    In all cases the Title is correct, but the Permalink is NOT. It keeps using the Permalink of the first Post in the main Query’s loop.

    I’ve tried using wp_reset_query before doing the work in the sidebar. I’ve tried setup_postdata() on each post within the sidebar get_posts(). I’ve tried passing the Post object rather than the Post ID. Nothing works — always gets the URL of the main query for the page.

    Why would WP sometimes not respect the ID value you pass to it in get_permalink?

    We’ve recently moved the site to a new server. Could there be an issue with the ability to write .htaccess etc.?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Okay, never mind. I figured it out.

    The standard get_permalink(ID) should work as advertised.

    The problem I have is specific to my site, and due to a recent customization I implemented with add_filter(‘post_link’, <function>). This is designed to adjust the permalink returned for certain conditions, and those conditions were the ones being encountered in the recent tests. I need to fix my filter code — it works for the main wp_query loop, but works incorrectly in get_posts loops (since it relies on the global $post).

    Doh! 🙂

    Yup. 🙂 Maybe it will be a clue for someone someday. Marking resolved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_permalink returns wrong URL’ is closed to new replies.