• I am really having trouble setting up a custom permalink structure for two custom post types. short explanation:
    Property – has custom taxonomy ‘property-location’
    Apartment – has ACF relationship field to choose a property the apartment belongs to
    So what I want is for the url to display:
    property: /all-locations/%location%/%postname%
    apartments: /all-locations/%location%/%property%/%postname%

    As the second one is more complicated, here is what works for the link:

    add_filter('post_type_link', 'apartments_permalink_structure', 10, 4);
    function apartments_permalink_structure($post_link, $post, $leavename)
    {
    	$post_id = $post->ID;
        if ( false !== strpos( $post_link, '%property%' ) ) {
        	$property = get_field('property_relation', false, false);
            $locations = get_the_terms( $property, 'property-location');
            $post_link = str_replace( '%location%', array_pop($locations)->slug, $post_link);
            $post_link = str_replace( '%property%', get_post_field('post_name', $property), $post_link );
        }
        return $post_link;
    }

    However, even after flushing rewrite rules, changing permalinks settings in backend and even hard-core flush it from the database, I get only 404 page. In the backend the correct permalink is visible, but somehow the theme now doesn’t recognise that it is a single-apartment.
    What do I need to do to make it visible again?
    BTW same on single-property – permalink is correct but single display not working…

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Have you added rewrite rules so WP knows which permalink term gets assigned to which query var? Use the “request” filter to examine how requests were assigned to query vars. The nature of an improper assignment should give a clue to what needs to be fixed.

    Thread Starter resqonline

    (@resqonline)

    I am actually not sure about the rewrite rules. For ugly permalinks it’s okay if there is only /?property=something or /?apartment=something. It’s only the nice urls that don’t work correctly, and I don’t have any index.php as in all the examples…
    It’s pretty confusing… all explanations that I found so far were different, so no idea how to actually set this up correctly

    I used the Query Monitor plugin to figure out the rewrite rules and have added the following rewrite rules:

    add_rewrite_rule('all-locations/([^/]+)/?$', 'property-location=$matches[1]', 'top');
        add_rewrite_rule('all-locations/([^&]+)/([^&]+)/?$','location=$matches[1]&property=$matches[2]','top');
        add_rewrite_rule('all-locations/([^&]+)/([^&]+)/([^/]+)/?$','location=$matches[1]&property=$matches[2]&apartment=$matches[3]','top');

    I tried both top and bottom, no difference.

    The first one for the location rewrites the url but the taxonomy template (taxonomy-property-location.php) is not used, instead the blog page is shown.
    The second one for the property works, it gets the correct single-property.php template file.
    The third for apartment rewrites the url, but once again wrong template file. is_single() and is_singluar() both are true, but it doesn’t get the template single-apartment.php but the single-property.php

    Not sure how to proceed from here…

    Moderator bcworkz

    (@bcworkz)

    No index.php? How can that be? It’s fundamental to how WP works. Even the pretty permalink rewrite rules in .htaccess utilize index.php. It should be in the root folder of your WP installation. All it does is initiate the WP environment. WP figures out the rest through URL parameters and permalink terms. With a properly configured server, when no file is specified, the request goes to index.php anyway. That is why your rewrites sort of work without index.php.

    I suggest you provide the file in the rewrite rules, it’ll save a little processing with each request.

    When you rewrite terms as custom URL parameters, you need to whitelist those parameters through the “query_vars” filter. Add your parameters/vars to the passed array before returning the entire, updated array. Then your arguments will be available as query vars in the “pre_get_posts” action. Ideally, whitelisting alone will resolve the problems with wrong template, etc. If not, adjusting the query vars in “pre_get_posts” should solve the problem. If worst comes to worst, you can force the template choice through the “template_include” filter.

    Thread Starter resqonline

    (@resqonline)

    Of course I have a index.php file, I meant in the url, when I set it to standard permalinks there is no index.php in my urls.

    Okay so I have to add query vars inside the templates itself… was hoping it would work without that, makes everything so much more complicated ??

    Thread Starter resqonline

    (@resqonline)

    This is really weird… now I managed to rewrite the Apartment URL with

    add_rewrite_rule('^all-locations/([^/]*)/([^/]*)/([^/]*)$', 'index.php?apartment=$matches[3]', 'top');

    the important part is the ^ at the beginning, otherwise this doesn’t work. Also I only matched the 3rd part and removed all other matching rules because it woulnd’t work.

    However, the link for the property post type now doesn’t get created correctly, the weird thing is, with the intended url /all-locations/locationname/propertyname I actually get the correct template, but the permalinks apparently don’t get the term slug… this worked before, no idea why it doesn’t work now…

    the rewrite rule for the property is now:
    add_rewrite_rule('^all-locations/([^/]*)/([^/]*)$','index.php?property=$matches[2]','top');
    once again with ^ at the beginning. When I remove this ^ then the apartment link (see above) doesn’t work.

    This is really frustrating!

    • This reply was modified 7 years, 7 months ago by resqonline.
    Moderator bcworkz

    (@bcworkz)

    You’re in good company if you find rewrite rules baffling and frustrating. The ^ and $ are usually important because they determine how many permalink terms there are. In your case, for the same base term, there is a different query (I think) for 3 term links compared to 4 term links. Regardless of how many terms there are in the link, you should only pass the minimum needed to get the resource correctly queried. For posts, the slug alone should suffice. You’ve pretty much discovered as much yourself.

    I’m not sure I’m following what the current problems are. It sounds like the problem now is the links. If you were to manually type in an URL, do you get the correct results?

    You still typically need to filter the post links on output through the ‘post_type_link’ filter. Obviously you replace all %tags% with the proper terms, but you can also modify the resulting link in any other way needed. Ensure the links created are compatible with the rewrite rules you’ve added.

    In case you are having trouble with queries too, there are three principle places where you can verify what’s happening and adjust accordingly. The first stop is the ‘request’ filter. The passed array should contain only the query vars you set in the rewrite rule. The next stop is the ‘pre_get_posts’ action. Here WP has parsed the request and set all the supporting query vars based on the request. If any needed custom query vars are missing, it’s probably because they were not whitelisted in the ‘query_vars’ filter. Query vars in ‘pre_get_posts’ can be added or altered as needed.

    The last stop is the ‘posts_request’ filter. WP has digested all the query vars into an associated mySQL query string. You can often learn why a query is not working by examining the actual SQL query. It can also be modified if necessary, though most changes are better accomplished in the ‘pre_get_posts’ action.

    Of course there are many other actions and filters along the way, but these three are good places to check the progress of your request to verify all things are as they should be.

    Thread Starter resqonline

    (@resqonline)

    thanks so much for your help, I am having another go at this in the next days and hopefully will find a way to get this working

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom Permalinks with additional values – still 404 after rewrite flush’ is closed to new replies.