Skip Navigation

[Resolved] get_permalink() returning the wrong URL

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 1 year, 4 months ago.

Assisted by: Nigel.

Author
Posts
#2628625

I have a php snippet that redirect all views of the post type "robot" to the parent post type "manufacturer". There is a problem with the get_permalink() function I use in the php snippet.

Example: For the manufacturer "mir" the correct url is "hidden link" however the value returned by my snippet is "hidden link" dropping "manufacturer" from the permalink url.

here is the whole snippet:

add_action( 'template_redirect', 'redirect_post_type_single' );
function redirect_post_type_single(){
if ( ! is_singular( 'robot' ) )
return;
$mfg_id = toolset_get_related_post( get_the_ID(), 'manufacturer-robot', 'parent' );
wp_redirect( get_permalink( $mfg_id ), 301 );
exit;
}

Any ideas what is going wrong?

#2628711

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Have you debugged the code?

Have you checked that $mfg_id has the correct post ID for the manufacturing post (e.g. for the mir post)?

Have you checked what exactly is returned by get_permalink (rather than what the result of the wp_redirect call is)?

#2628773

Hi Nigel, thanks for the quick response. I have tried manually putting the number of the post_id instead of the $mfg_id variable. Same result. I have not tested the output of get_permalink(). I do not have much experience with debugging in WP, what is the easiest way to see the output?

#2628819

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor, but only when any messages are sent to the log (e.g. errors).

You can add a line to your code to send a value like so:

add_action('template_redirect', 'redirect_post_type_single');
function redirect_post_type_single()
{
    if (!is_singular('robot')) {
        return;
    }

    // $mfg_id = toolset_get_related_post(get_the_ID(), 'manufacturer-robot', 'parent');

    $mfg_id = 123;
    $link = get_permalink( $mfg_id );
    error_log( "Permalink is $link" );

    wp_redirect(get_permalink($mfg_id), 301);
    exit;
}

Change the ID (123) as needed.

#2628991

The problem went away, the original code is now working correctly. Not sure what was wrong, maybe some cache issue.