4

I have a CPT called 'experts', that has been created in a theme I bought, and I can't find out where nor where to change it. I need to change a parameter to 'with_front' => false Because my general permaling structure goes with /blog and I do'nt want experts to be in /blog/experts. Is there a way I could do that adding something in the functions file? I have tried this (How to set "with_front'=>false" to a plugin-generated cpt?) and various things, but could not get it to work. Thanks :)

5

You could try the newly register_post_type_args filter to adjust it.

Here's an untested example:

/**
 * Set 'with_front' to false for the 'experts' post type.
 */
add_filter( 'register_post_type_args', function( $args, $post_type )
{
    if( 'teachers' === $post_type && is_array( $args ) )
            $args['rewrite']['with_front'] = false;

    return $args;
}, 99, 2 );

Updated with new info from @Agnes: the post type is teachers not experts.

  • That sounds great, thanks a lot for the suggestion. However... it does not seem to work. I have added this to functions.php, I have revalidated permalinks, and the experts still come with 'blog/experts' – Agnes Apr 25 '16 at 7:13
  • YES! With 2 corrections, this works :) 1> I found out the real name of the plugin generated CPT was teachers and not experts 2> I simplified the IF. Code below. – Agnes Apr 28 '16 at 7:59
  • NB: I don't know why defining the specigfic post type does not work.Also, I dont know why it works in parent theme functions file and not child theme. – Agnes Apr 28 '16 at 8:10
  • Thanks for the update. Note that we must keep the post type check. I did a modification regarding the correct post type teachers and temporarily changed the isset check (since with_front is true by default) and changed the priority to 99. Does this make any difference? @Agnes – birgire Apr 28 '16 at 8:30
  • YES, it works perfectly now. Thanks @birgire :) – Agnes May 2 '16 at 14:48
2

Additionally, if the CPT has taxonomies associated with it, I've successfully used the following code to rewrite those as well:

/**
 * Set 'with_front' to false for the 'portfolio_category' post taxonomy.
 */

add_filter( 'register_taxonomy_args', function( $args, $taxonomy )
    {
        if( 'portfolio_category' === $taxonomy && is_array( $args ) )
            $args['rewrite']['with_front'] = false;
        return $args;
    }, 99, 2 );

In case that is helpful to anyone.

-2

This solution works, added to parent theme functions.php:

        add_filter( 'register_post_type_args', function( $args, $post_type )
        {
            $args['rewrite']['with_front'] = false;
            return $args;
        }, 10, 2 );     
  • 1
    Your code will affect to all post types. Not recommended. – cybmeta Apr 28 '16 at 8:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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