Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The format of the link is ok, it works. But when I open the post did not lead me to the single-customtypes but the 404 page.

add_action('init', 'tdd_add_rewrite_rules');
add_filter('post_type_link', 'tdd_permalinks', 10, 3);

function tdd_add_rewrite_rules()

{

    // Register custom rewrite rules

    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag('%pasager%', '([^/]+)', 'pasager=');
    $wp_rewrite->add_rewrite_tag('%z_localitatea2%', '([^/]+)', 'z_localitatea2='); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES
    $wp_rewrite->add_rewrite_tag('%z_destinatia2%', '([^/]+)', 'z_destinatia2='); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES

    $wp_rewrite->add_permastruct('pasager', '/pasager/%z_localitatea2%-%z_destinatia2%-%post_id%.html', false); //[EDIT] TO ADD MORE SLUGS, ADD /%VARIABLE2% BETWEEN %VARIABLE1% & %POST_TYPE%

}

function tdd_permalinks($permalink, $post, $leavename)
{

    $no_data = 'no-data';

    $post_id = $post->ID;

    if($post->post_type != 'pasager' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))

        return $permalink;

    $var1 = get_post_meta($post_id, 'z_localitatea2', true); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES
    $var2 = get_post_meta($post_id, 'z_destinatia2', true); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES

    $var1 = sanitize_title($var1); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES
    $var2 = sanitize_title($var2); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES

    //[COPY] COPY THE BELOW LINES TO ADD MORE VARIABLES 
    if(!$var1)
            $var1 = $no_data;

    //[COPY] END COPY HERE
    if(!$var2)
            $var2 = $no_data;

    //[COPY] END COPY HERE
    if(!$var3)
        $var3 = $post_id;

    //[COPY] END COPY HERE

    $permalink = str_replace('%z_localitatea2%', $var1, $permalink); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES
    $permalink = str_replace('%z_destinatia2%', $var2, $permalink); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES
    $permalink = str_replace('%post_id%', $var3, $permalink); //[COPY] COPY THIS LINE TO ADD MORE VARIABLES

    return $permalink;

}

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
            register_post_type( 'pasager',
                    array(
                            'labels' => array(
                                    'name' => __( 'Pasager' ),
                                    'singular_name' => __( 'Pasager' )
                            ),
                    'public' => true,
                    'has_archive' => true,
                    'rewrite' => false,
                    )
            );
    }
        $args = array(

    );

        register_post_type( 'pasager', $args );

   function post_type_labels( $singular, $plural = '' )
{
    if( $plural == '') $plural = $singular .'s';

    return array(
        'name' => _x( $plural, 'post type general name' ),
        'singular_name' => _x( $singular, 'post type singular name' ),
        'add_new' => __( 'Add New' ),
        'add_new_item' => __( 'Add New '. $singular ),
        'edit_item' => __( 'Edit '. $singular ),
        'new_item' => __( 'New '. $singular ),
        'view_item' => __( 'View '. $singular ),
        'search_items' => __( 'Search '. $plural ),
        'not_found' =>  __( 'No '. $plural .' found' ),
        'not_found_in_trash' => __( 'No '. $plural .' found in Trash' ),
        'parent_item_colon' => ''
    );
} 
share|improve this question
2  
Please pay attention to code formatting. "Problem with adding rewrite rules when using permalink redirection?" isn't really a question. It is a sentence fragment with a question mark at the end. What, exactly, is the problem? Provide a description of the issue in your question. –  s_ha_dum Apr 20 '13 at 13:36
    
s_ha_dum some idea ? –  Blajan Marcos Apr 21 '13 at 14:51
    
You still have not added enough information. Why are you trying to manually alter the permalink of a CPT, for one? –  s_ha_dum Apr 21 '13 at 14:54
    
We have added new fields for custom types pasager. And I want two fields from there to define the permalinks. If no manual, how can I set? –  Blajan Marcos Apr 21 '13 at 15:09
    
Does this work if you remove your rewrite rules? –  s_ha_dum Apr 21 '13 at 15:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.