Jake Vanderwerf
9 days ago 47e77f9fac1155c536b2b87fec552c7fcce66fa6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace JVBase\registrar\helpers;
 
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
use WP_Post;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class MakeTimelineType {
    protected string $slug;
    protected string $postType;
    protected Registrar $registrar;
    public function __construct(string $slug, Registrar $registrar) {
        $this->slug = $slug;
        $this->postType = jvbCheckBase($slug);
        $this->registrar = $registrar;
 
        add_action('template_redirect', [$this, 'redirectChildToParent']);
    }
 
    /**
     * Set $this->is_timeline
     * @return void
     */
    public function redirectChildToParent(): void
    {
        if (!is_singular($this->postType)) {
            return;
        }
 
        global $post;
 
        // If this post has a parent, redirect to parent
        if ($post->post_parent) {
            $parent_url = get_permalink($post->post_parent);
 
            // Add anchor or query param to indicate which child was accessed
            $redirect_url = add_query_arg('update', $post->ID, $parent_url);
 
            wp_redirect($redirect_url, 301);
            exit;
        }
    }
}