1

I have a domain domain.com and subdomain materials.domain.com

At the domain.com scope, I created a custom post type called resources and a portfolio grid (using Elementor) to display it's featured image with the title.

When clicking on the grid item, the single post is shown for example: domain.com/resources/resource-title

I'd like to remove the single post from the domain and redirect on click to materials.domain.com/resource-title. I already have this page on the materialssubdomain).

How can I achieve that?

7
  • How are you generating the grid item? Please add that code to your question. – jdm2112 Jul 6 '20 at 15:08
  • I'm using elementor portfolio widget to display resource archive CPT as grid – user1301037 Jul 6 '20 at 15:26
  • So you need a redirect so that browsing to domain.com/resources/resource-title redirects you immediately to materials.domain.com/resource-title ? – mozboz Jul 6 '20 at 15:44
  • Yes. If possible I want to disable the single from the CPT at domain.com and when click on the grid Item, access materials.domain.com/resource-title (in practice if I can be able to setu up a custom destiny url at domain.com works too). – user1301037 Jul 6 '20 at 15:59
  • I read at this thread how redirect the single. Any idea how can I pass a custom url to the function? (maybe using custom fields?) – user1301037 Jul 6 '20 at 16:10
4
+50

Sometimes this would be achievable with add_rewrite_url but it looks like in this case it's not as you need to redirect to a different domain.

In this case you need .htaccess or nginx rules to do this, so you need to add something like this to your nginx config for domain.com only, in order to rewrite those URLs:

rewrite ^/resources/(.+) https://materials.domain.com/$1 permanent;

You'll likely need to restart nginx after you add this to the config file.

Happy to help if this doesn't do exactly what you want. There's more examples of nginx rewrite rules here: https://www.thegeekstuff.com/2017/08/nginx-rewrite-examples/

2

If you want to do it from WordPress environment itself..

function wpse370481_redirect_url() {
    if ( is_singular( 'resources' ) ) {
        global $post;
    
        $path = $post->post_name;
        
        $redirect_url = 'https://materials.domain.com/' . $path;
        
        wp_redirect( $redirect_url );
        exit;
            
    }
}
add_action( 'template_redirect', 'wpse370481_redirect_url' );   

Havn't tested this code as I currently don't have a setup to test this, but in theory, the code should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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