permalinks - Rename Custom Post Slug using taxonomy

admin2025-06-06  7

We have a custom posts called "Reviews" and rename the URL based on a taxonomy (manufacturers) selected for the post. For example if the MFG taxonomy called BENQ is selected the URL for the post will be

domain/benq/postname not domain/reviews/postname/

I'm getting this error: Warning: Missing argument 3 for custom_post_type_link(), called in /../wp-includes/class-wp-hook.php on line 288 and defined in /../wp-content/themes/understrap-child-theme/functions.php on line 1192

Here is the function:

add_filter('post_type_link', 'custom_post_type_link', 10, 2);

function custom_post_type_link($permalink, $post, $leavename) {
    if (!gettype($post) == 'post') {
        return $permalink;
    }

    $url_components = parse_url($permalink);
    $post_path = $url_components['path'];
    $post_path = trim($post_path, '/');
    $post_name = explode('/', $post_path);
    $post_name = end($post_name);

    if (!empty($post_name)) {

        if ($post->post_type == 'reviews') {

            $terms = get_the_terms($post->ID, 'manufacturers');

            if (is_array($terms)) {

                $term = array_pop($terms)->slug;
            }
            else {

                $terms = get_the_terms($post->post_parent, 'manufacturers');

                if (is_array($terms)) {
                    $term = array_pop($terms)->slug;
                }
                else {
                    $term = 'review';
                }
            }

            $permalink = str_replace($post_path, '' . $term . '/' . $post_name . '', $permalink);
        }
    }

    return $permalink;
}

Any idea of the issue?

We have a custom posts called "Reviews" and rename the URL based on a taxonomy (manufacturers) selected for the post. For example if the MFG taxonomy called BENQ is selected the URL for the post will be

domain/benq/postname not domain/reviews/postname/

I'm getting this error: Warning: Missing argument 3 for custom_post_type_link(), called in /../wp-includes/class-wp-hook.php on line 288 and defined in /../wp-content/themes/understrap-child-theme/functions.php on line 1192

Here is the function:

add_filter('post_type_link', 'custom_post_type_link', 10, 2);

function custom_post_type_link($permalink, $post, $leavename) {
    if (!gettype($post) == 'post') {
        return $permalink;
    }

    $url_components = parse_url($permalink);
    $post_path = $url_components['path'];
    $post_path = trim($post_path, '/');
    $post_name = explode('/', $post_path);
    $post_name = end($post_name);

    if (!empty($post_name)) {

        if ($post->post_type == 'reviews') {

            $terms = get_the_terms($post->ID, 'manufacturers');

            if (is_array($terms)) {

                $term = array_pop($terms)->slug;
            }
            else {

                $terms = get_the_terms($post->post_parent, 'manufacturers');

                if (is_array($terms)) {
                    $term = array_pop($terms)->slug;
                }
                else {
                    $term = 'review';
                }
            }

            $permalink = str_replace($post_path, '' . $term . '/' . $post_name . '', $permalink);
        }
    }

    return $permalink;
}

Any idea of the issue?

Share Improve this question edited Nov 11, 2018 at 11:41 cameronjonesweb 9201 gold badge12 silver badges16 bronze badges asked Nov 11, 2018 at 6:05 RWGRWG 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You're passing 3 arguments to your function ($permalink, $post, $leavename), but you're only specifying that it has 2 when you're adding the filter (the last parameter to add_filter). Change your add_filter call to this:

add_filter( 'post_type_link', 'custom_post_type_link', 10, 3 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749192488a317135.html

最新回复(0)