• Resolved kyle-tully

    (@kyle-tully)


    I’ve got Pods installed and working on a custom post type.

    However since installing my Pages throw 404 errors.

    All posts (using the custom post type or not) work and so do categories.

    Any ideas what is causing this?

    Thanks in advance!

    Kyle

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Paul Clark

    (@pdclark)

    Hi Kyle,

    Unfortunately I’m not able to recreate this issue. Some things to consider:

    • Do Pages come back if you disable the Pods plugin?
    • Do Pages come back if you save Permalink structure under Settings > Permalinks?
    • Did you by chance change Custom Rewrite Slug under Edit Pod > Advanced Options? For example, changing this value to / could cause a Custom Post Type to override conflicting URLs from Pages.
    Thread Starter kyle-tully

    (@kyle-tully)

    Yes
    No
    Yes!

    I did have / set as custom rewrite. I thought this would only apply to the custom post type. If I remove this the pages work again. Thanks for the heads up.

    Now to figure out how to make these two work nicely together…

    Plugin Support Paul Clark

    (@pdclark)

    Ah! Great! A lucky guess.

    Having two post types with a root rewrite rule gets a bit tricky, because Rewrites are basically an array of regexes —?the first regex to match the URL wins. So if two post types (pages and another) get registered with / as the rewrite base, whichever is first will be chosen over the other.

    Which rewrite matches can most easily be seen in the Debug This plugin menu: Debug This > Query > Rewrites, the order of which can be edited with the rewrite_rules_array filter.

    But that doesn’t solve the problem of two post types having the same rewrite path. For that, pre_get_posts is a solution. In a case where both Pages and a Custom Post Type called “product” have a rewrite base of /, I was able to get both to load with the following code:

    
    add_action( 'pre_get_posts', function( $q ) {
    	if (
    		$q->is_main_query()
    		// If this installation is in a subdirectory,
    		// or a subdirectory multisite, then below [1] would be [2]
    		&& explode( '/', $_SERVER['REQUEST_URI'] )[1] === $q->query['name']
    	) {
    		$q->set(
    			'post_type',
    			[
    				// Post Types with rewrites set to '/' (root) directory
    				'page',
    				'product',
    			]
    		);
    	}
    });
    
    • This reply was modified 3 years, 8 months ago by Paul Clark.
    Thread Starter kyle-tully

    (@kyle-tully)

    Epic! I had to add another test to the if statement because it was throwing an undefined index error on homepage/categories etc.

    This is what worked for me using a custom post type called review:

    add_action( 'pre_get_posts', function( $q ) {
    	if (
    		$q->is_main_query()
    		&& $q->is_single() 
    		// If this installation is in a subdirectory,
    		// or a subdirectory multisite, then below [1] would be [2]
    		&& explode( '/', $_SERVER['REQUEST_URI'] )[1] === $q->query['name']
    	) {
    		$q->set(
    			'post_type',
    			[
    				// Post Types with rewrites set to '/' (root) directory
    				'page',
    				'review',
    			]
    		);
    	}
    });

    Thanks so much for your help with this.

    Plugin Support Paul Clark

    (@pdclark)

    Great!
    Glad to hear you found the solution and thanks for sharing your fix!
    Have a great day! ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘404 on pages after installing pods’ is closed to new replies.