Home Forums Plugin Support WP User Frontend Pro Example / Snippet: Redirect based on post_type / get_post_meta

This topic is: resolved

This topic contains 4 replies, has 2 voices, and was last updated by  towhid 4 years, 5 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #34208

    Dan
    Member
    Post count: 13

    I was having some issues figuring out how to do redirects based on post_type. Here's a nifty snippet that you can use to get more control over redirects. I also included a way to find a related post from a custom meta field.

    //Redirect WPUF Version: 2.2.6
    function wpuf_force_redirection($response, $post_id) {
     
            $post_type = get_post_type($post_id);
           
            if($post_type == 'type1'){
       
            //get parent post
            $parent_id = get_post_meta($post_id, 'related_id', true);
           
            //If parent found, update $response
            if ($related_id) {
                    //Update Response Vars
                    $response['redirect_to'] = get_permalink($related_id);
            }
           
        }elseif($post_type == 'type2'){
       
            //Update response to new post url
            $response['redirect_to'] = get_permalink($post_id);
           
        }
        return $response;
    }
    add_filter('wpuf_add_post_redirect', 'wpuf_force_redirection', 10, 2); //10: priority, 2: accepted vars
    add_filter('wpuf_update_post_redirect', 'wpuf_force_redirection', 10, 2);
    #34335

    towhid
    Participant
    Post count: 4505

    Hello Dan,

    Would you please let me know where you want to actually implement this code? Have you tried this code for you ? Are you getting any error?

    Thank you ๐Ÿ™‚

    #34337

    Dan
    Member
    Post count: 13

    I was thinking you could add this to the documentation. There are examples for the other filters, but not redirection. You could add this as an example to help out users.

    #34338

    Dan
    Member
    Post count: 13

    There was a typo, here is the fixed version:

    //Redirect WPUF Version: 2.2.6
    function wpuf_force_redirection($response, $post_id) {
     
            $post_type = get_post_type($post_id);
           
            if($post_type == 'type1'){
       
            //get parent post
            $related_id = get_post_meta($post_id, 'related_id', true);
           
            //If parent found, update $response
            if ($related_id) {
                    //Update Response Vars
                    $response['redirect_to'] = get_permalink($related_id);
            }
           
        }elseif($post_type == 'type2'){
       
            //Update response to new post url
            $response['redirect_to'] = get_permalink($post_id);
           
        }
        return $response;
    }
    add_filter('wpuf_add_post_redirect', 'wpuf_force_redirection', 10, 2); //10: priority, 2: accepted vars
    add_filter('wpuf_update_post_redirect', 'wpuf_force_redirection', 10, 2);
    #34341

    towhid
    Participant
    Post count: 4505

    Hello Dan,

    Thank you so much!!. I will provide this snippet of code to our developer team. ๐Ÿ™‚

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Example / Snippet: Redirect based on post_type / get_post_meta’ is closed to new replies.