generate custom permalink based on a custom field

admin2025-01-08  4

I'm newbie to the Wordpress.

So far I have a custom post type called 'property'. This post type has one custom field called 'property_city'.

In WP-admin - permalink setting, I have selected Post name option. So, posts of this custom post type have URLs like:

/properties/edge-apartments/

/properties/northgate-point/

What I want to have those URL to be generated using the city of the property and also I want to change properties prefix with student-properties like that:

/student-properties/Manchester/edge-apartments/

/student-properties/Chester/northgate-point/

I have tried various plugin and various code, but none of them worked as expected.

Any help would be appreciated.

Thanks,

I'm newbie to the Wordpress.

So far I have a custom post type called 'property'. This post type has one custom field called 'property_city'.

In WP-admin - permalink setting, I have selected Post name option. So, posts of this custom post type have URLs like:

http://xxx.local/properties/edge-apartments/

http://xxx.local/properties/northgate-point/

What I want to have those URL to be generated using the city of the property and also I want to change properties prefix with student-properties like that:

http://xxx.local/student-properties/Manchester/edge-apartments/

http://xxx.local/student-properties/Chester/northgate-point/

I have tried various plugin and various code, but none of them worked as expected.

Any help would be appreciated.

Thanks,

Share Improve this question edited Feb 11, 2017 at 9:55 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 11, 2017 at 7:23 Parth VoraParth Vora 1411 silver badge4 bronze badges 3
  • Do you have any particular reason to implement it with custom field? If URL depends on a custom field, then instead of custom field, you may implement it using category or custom taxonomy that acts like category. That'll be much better overall. – Fayaz Commented Feb 11, 2017 at 8:43
  • It's client requirement. Project development is near to end, so it is not possible to make that change now. – Parth Vora Commented Feb 11, 2017 at 8:47
  • Post type slug is set when you register the post type, please edit your question to show how you register your post type. – Milo Commented Feb 11, 2017 at 16:35
Add a comment  | 

2 Answers 2

Reset to default 1

To generate custom permalink based on custom field, try the following steps: -

Step 1: Rewrite the Custom Post Type Slug

Assuming you are a developer on this project, rewrite the slug for custom post type while registering it. Set the slug to any-keyword-or-cpt-base/%custom-field-name%

See the following example for your case. In this example, I used student-properties as CPT slug base for permalinks and then a slash / followed by modifier as %city% which will be replaced in step 2 with actual value of city for each post permalink.

$rewrite = array(
    'slug'  => 'student-properties/%city%',
    'with_front'  => true,
    'pages' => true,
    'feeds' => false,
);
$args = array(
    'label'                 => __( 'Properties', 'ds' ),
    'description'           => __( 'Properties', 'ds' ),
    //  'labels'                => $labels, 
    //  'supports'              => array( 'title', 'editor', 'thumbnail', 'page-attributes' , 'custom-fields'),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_icon'             => 'dashicons-excerpt-view',
    'menu_position'         => 2,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'rewrite'               => $rewrite,
    //'capabilities'          => $capabilities,
    'show_in_rest' => true
);
register_post_type( 'properties', $args );

Step 2: Add filter for post_type_link

To replace the %custom-field-name% that was previously set in rewrite slug of custom post type, we need to filter the output of post permalink in the following way: -

function digitalsetups_single_property_permalink( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $property_city = get_post_meta($post->ID, 'property_city', true);
        if($property_city) {
            return str_replace( '%city%' , $property_city , $post_link );

        }
    }
    return $post_link;  
}
add_filter( 'post_type_link', 'digitalsetups_single_property_permalink', 1, 3 );

In the above code, the custom field name or identity is property_city. We get the property_city value and if its not false (you can add additional validations here), we replaced %city% with the property_city value.

Notes & Tips:

  1. These steps are for custom field.

  2. In case you want to add taxonomy term value in the rewrite slug, get the terms for the taxonomy in the filter and replace with term slug. Additionally, just for taxonomy case & to ensure consistency in URLs, set the taxonomy rewrite slug to same as CPT base that you used in CPT rewrite slug. Let me know if this is the case. I'll give code example and more clarification.

  3. If you are using a plugin to rewrite CPT base you can set base from there e-g: student-properties/%city% and then in replace %city% to your value either from custom taxonomy or custom field or something else via post_type_link filter - See step 2.

Try this plugin Custom Post Type Permalinks, it allows you to set permalinks for custom post types. If your client does not want additional plugin then this plugin is also available on GitHub you can fork it or include this functionality in your own plugin or your functions.php.
In order to use anything dynamic in the permalink, it has to be a Structure Tag e.g. Custom Taxonomies, Author Name, Date Published, etc.
So you need to have a taxonomy called 'City' which you will select while creating the post and then you can use the taxonomy in the Setings->Permalinks ->

%city%/%postname%



Look at this screenshot for a better understanding:

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270978a1476.html

最新回复(0)