Skip Navigation

[Resolved] Toolset Custom Fields in Php not Working

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by ioannisM-2 6 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#590973

Hello,

I use a function that replaces the post slug in url with a post's field. But unfortunatelly this does not work with fields that have been created with toolset.

Here is my code that works only with native wordpress custom fields

function my_custom_permalink( $url, $post, $leavename ) {
    $url= trailingslashit( home_url('/post/'. $post->ID .'/'. $post->test .'/' ) );
    return $url;
}
add_filter( 'post_link', 'my_custom_permalink', 10, 3 );



add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
    $new_rules['^post/([0-9]+)/([^/]+)/?'] = 'index.php?p=$matches[1]';
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
}

If I replace the wordpress field "test" with "wpcf-my-test", the toolset field does not appear in url and I am redirected to a 404 Error page. Is there any way that I could fix that?

Thank you!

#591093

Dear ioannis,

It is a custom PHP codes problem, you can get the custom field value using wordpress function get_post_meta(), and use the value into your custom codes, for example, modify this line from:

    $url= trailingslashit( home_url('/post/'. $post->ID .'/'. $post->test .'/' ) );

To:

    $my_test = get_post_meta($post->ID, 'wpcf-my-test', true); // get the value of custom field
    $url= trailingslashit( home_url('/post/'. $post->ID .'/'. $my_test .'/' ) );

More help:
https://codex.wordpress.org/Function_Reference/get_post_meta

#591845

Luo,

Thank you, that was very helpful! Now the rewrite works fine but the issue is that it works only for the posts. I need the slugs to be changing if specific conditions exist. If I replace the slug of taxonomy term with the taxonomy term's title or custom post type post's slug with toolset custom field then a 404 error page appears.

Here is the code that I use

add_filter( 'term_link', 'change_term_slug', 10, 2 );

function change_term_slug( $permalink, $term ) {
  $terms = get_terms($taxonomy, $args );
  
     $permalink = trailingslashit( home_url('/term/'. $term->term_id .'/'. $term->name .'/' ) );
    return $permalink;
}




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

function change_employee_slug( $url, $post ) {
    if ( 'employee' == get_post_type( $post ) ) {
      
  $new_slug = get_post_meta($post->ID, 'wpcf-new-slug', true); 
    $url= trailingslashit( home_url('/post/'. $post->ID .'/'. $new_slug .'/' ) );
    }
    return $url;
}

So my questions are:
1) For the taxonomy the replacement worked just with the native term's title. How could I use the toolset field wpcf-new-term-slug in php? I tried but nothing changed...

2) Why do I see a 404 error page in both cases, although the slug in url is correctly changing? Am I doing sth wrong?

#591863

Please create new thread for the new questions, that will help other users to find the answers.

Q1) For the taxonomy the replacement worked just with the native term's title. How could I use the toolset field wpcf-new-term-slug in php?
To get terms attached to the post., please try wordpress function get_the_terms():
https://developer.wordpress.org/reference/functions/get_the_terms/
Retrieve the terms of the taxonomy that are attached to the post.

Q2) Why do I see a 404 error page in both cases, although the slug in url is correctly changing? Am I doing sth wrong?
It should be a problem of you custom PHP codes, I suggest you debug your codes line by line.
and it is out the range of Toolset support, you can also check our Toolset Contractors:
https://toolset.com/contractors/

#593007

Ok Luo,I understand. I will try to debug my php code line by line. I am searching online for tutorials about how I can do that but I cannot find sth about debugging line by line. Could you please point me to the right direction or give me a link from a source with more info about that?

Thank you!

#593142

The easiest way, is remove all the custom PHP codes you mentioned above, then add them back line by line, try to locate the problem codes.

#593420

I see, it's so simple. Thank you Luo!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.