6

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 :)

3 Answers 3

9

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.

6
  • 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, 2016 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, 2016 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, 2016 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, 2016 at 8:30
  • I wonder what is 99 for?
    – Agnes
    May 2, 2016 at 14:48
3

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
  • 2
    Your code will affect to all post types. Not recommended.
    – cybmeta
    Apr 28, 2016 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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