I would like to do something special for a website in Wordpress. I have a "custom post type" whose data are all "post custom fields" except the name of the post. I need to create multiple urls for each of the "post custom fields" of the same post, and shows custom post field value.

For example

The custom post type is custompostA This custom post type has the following "custom post fields": customfieldA, customfieldB

I add a new post whose title is "Example1"

Based on this, I want two different urls

www.domain.com/custompostA/Example1/customfieldA www.domain.com/custompostA/Example1/customfieldB

The first URL will display the customfieldA value and the second URL will display customfieldB value

I hope I have been clear since my exposure.

Thank you very much Javier

  • are there just the two fields, or might they vary and be numerous? – Milo May 12 '14 at 18:17
  • No, there are multiple fields (about 20 or 25). – JavierCabot May 12 '14 at 18:35

You can add a query var and a rewrite rule to handle the extra URL data-

Add the wpd_cf query var to the list of known query vars:

function wpd_add_query_var( $query_vars ){
    $query_vars[] = 'wpd_cf';
    return $query_vars;
add_filter( 'query_vars', 'wpd_add_query_var' );

Add a rewrite rule to handle the incoming requests and set the wpd_cf query var. This particular rule will match upper and lower case alphabet plus a hyphen. WordPress has a default rule in place to capture numbers after the post name for multi-page posts, so this will avoid capturing those requests. The first argument should match the URL slug of your custom post type, the second argument should set the key you registered your custom post type under.

function wpd_add_rewrite_rule(){
add_action( 'init', 'wpd_add_rewrite_rule' );

Then in the template (or a template filter) you can check the value of wpd_cf:

$the_requested_field = get_query_var( 'wpd_cf' );

Don't forget to flush rewrite rules after making any changes, this can be done by visiting the Permalinks Settings page in admin.

|improve this answer|||||
  • Thanks Milo. My custom post type it's "creacion-empresas". My post name it's "espana" and my custom field name it's "forma_juridica". When i put "domain.com/creacion-empresas/espana/forma_juridica" I recive 404 page – JavierCabot May 16 '14 at 17:20
  • you have to add the underscore to the regex pattern if you want to match that: [_-A-Za-z] – Milo May 16 '14 at 17:53
  • Nice it works. But i have a problem. Link canononial it's the same for all custom fields. What can i do? If i leave this, google only indexes one post ant not indexes custom fields – JavierCabot May 17 '14 at 22:10
  • You'll have to remove the rel_canonical action from wp_head and add your own function to output it. Look in wp-includes/default-filters.php to see where the original is added, and wp-includes/link-template.php to see the original function. – Milo May 18 '14 at 0:41

Well... you COULD do this by using the Pods plugin. it would require a recode of what you are doing but Pods native support of wildcard support would make this possible and wouldn't take too much code to implement.

|improve this answer|||||
  • I cannot install Pods Plugin. This plugin makes an SQL error on my website – JavierCabot May 16 '14 at 17:21
  • What's the error? – Christopher Carvache May 16 '14 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.