• Hi,

    Can you help me out with this problem?
    I want to rewrite custom post type – mieszkania, so they have url like so:

    /inwestycja/176-kamienica/19009/ (where number is mieszkanie’s ID).

    So what i got is that code in register post for mieszkanie.

    'rewrite' => array(
    			'with_front' => false,
    			'slug' => '/inwestycja/%mieszkanie_slug%'
    		)

    and this code for dynamic url change. But it doesn’t work with your plugin. (it works in native WP with post_type_link)

    Any idea why ?

    //Rewrite rule 
    function change_apartment_url( $url, $post ) {
        if ( 'mieszkanie' == get_post_type( $post ) ) {
    		$inwestycja = get_field('inwestycja', $post-ID);
    		$inwestycjaObj = get_post($inwestycja);
    		//$inwest_slug = 'lofthaus-zablocie';
    		$inwest_slug = $inwestycjaObj->post_name;
    		return str_replace('%mieszkanie_slug%', $inwest_slug, $url);
        }
        return $url;
    }
    add_filter( 'permalink_manager_filter_permastructure', 'change_apartment_url', 5, 2 );
Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Maciej Bis

    (@mbis)

    Hi @mastafu,

    Please keep in mind that the custom permalinks used by my plugin are stored statically and are not automatically updated if permastructure format is changed.

    Could you try to use “Tools -> Regenerate/reset” functionality and regenerate “mieszkanie” items?

    You can also simplify your code a bit ??

    function change_apartment_url( $url, $post ) {
        if ( $post->post_type == 'mieszkanie' ) {
    			$inwestycja = get_field('inwestycja', $post->ID);
    
    			if( !empty($inwestycja) ) {
    				$inwestycjaObj = get_post($inwestycja);
    				return str_replace('%mieszkanie_slug%', $inwestycjaObj->post_name, $url);
    			}
        }
        return $url;
    }
    add_filter( 'permalink_manager_filter_permastructure', 'change_apartment_url', 5, 2 );
    Thread Starter Mastafu Design

    (@mastafu)

    Thx Maciek,

    As to regeneration – No slugs were updated! for mieszkania.

    Thread Starter Mastafu Design

    (@mastafu)

    But I think, now after saving post, slug is being updated. Will check it more and let you know. thx.

    Thread Starter Mastafu Design

    (@mastafu)

    Unfortunately still not luck.
    Even when I change it to static test.

    //Rewrite rule 
    function change_apartment_url( $url, $post ) {
        if ( $post->post_type == 'mieszkanie' ) {
    			//$inwestycja = get_field('inwestycja', $post->ID);
    			$inwestycja = "test";
    			
    			if( !empty($inwestycja) ) {
    				$inwestycjaObj = get_post($inwestycja);
    				return str_replace('%mieszkanie_slug%', $inwestycjaObj->post_name, $url);
    			}
        }
        return $url;
    }
    add_filter( 'permalink_manager_filter_permastructure', 'change_apartment_url', 5, 2 );

    Saving or regenerating is not replacing %mieszkanie_slug% with proper slug.
    Frustrating as this is the last bit needed to finish my clients project ??

    'rewrite' => array(
    			'with_front' => false,
    			'slug' => '/inwestycja/%mieszkanie_slug%'
    			//'slug' => '/inwestycja/lofthaus-zablocie'
    		)
    Thread Starter Mastafu Design

    (@mastafu)

    Same with static $url = ‘test’; at the end of the function. It is just not working …

    Plugin Author Maciej Bis

    (@mbis)

    Hi @mastafu,

    I forgot to mention that hook you are using ‘permalink_manager_filter_permastructure’ will not be applied to the permalink format you set with [‘rewrite’][‘slug’] parameter.

    The snippet will exclusively work with the permastructure settings defined in Permalink Manager settings:
    https://permalinkmanager.pro/docs/basics/bulk-edit-wordpress-permalinks/

    Thread Starter Mastafu Design

    (@mastafu)

    Looks like your plugin reads rewrite slug as it has detected it correctly
    Mieszkanie – > inwestycja/%mieszkanie_slug%/%mieszkanie%

    Any more ideas?
    Or maybe this can be done with native post_type_link ?

    Plugin Author Maciej Bis

    (@mbis)

    Hi @mastafu,

    Could you send me the screenshot with your “Permastructures” settings (Permalink Manager section)? Even if the rewrite slug is correctly displayed, you will still need to make my plugin save the native as custom permastructure. To do so, simply click “Save permastructures” button on “Permastructures” page.

    Plugin Author Maciej Bis

    (@mbis)

    You can also try another filter:

    function pm_extra_permastructure_tags($default_uri, $native_slug, $post, $slug, $native_uri) {
      // Do not affect native URIs
      if($native_uri == true || empty($post->post_type) || $post->post_type !== 'mieszkanie') { return $default_uri; }
    
    	$inwestycja = get_field('inwestycja', $post->ID);
    
    	if( !empty($inwestycja) ) {
    		$inwestycjaObj = get_post($inwestycja);
    		
    		$default_uri = str_replace('%mieszkanie_slug%', $inwestycjaObj->post_name, $default_uri);
    	}
      
      return $default_uri;
    }
    add_filter('permalink_manager_filter_default_post_uri', 'pm_extra_permastructure_tags', 3, 5);
    Thread Starter Mastafu Design

    (@mastafu)

    You did it, thank you very much!
    Super robota Maciek, to mnie trapi?o od 2 dni … ??

    Plugin Author Maciej Bis

    (@mbis)

    Hi @mastafu,

    No problem, I am happy to help. I should mention ‘permalink_manager_filter_default_post_uri’ hook in the very beginning ??

    Mi?ego wieczoru ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Custom rewrite rule based on other custom post type title’ is closed to new replies.