Jake Vanderwerf
2026-05-01 48721c85ebcfa973ee81719d2467ca80e4253dc9
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * @param string $page
 * @param string $field
 *
 * @return string
 */
function jvbContactPage(string $page = 'contact', string $field = 'url'):string
{
    $out = get_option('jvbContactPage');
    if (!$out) {
        $out = array();
        $contact = new WP_Query(array(
            'post_type'     => 'page',
            'title'         => 'Contact',
            'fields'        => 'ids'
        ));
        if ($contact->have_posts()) {
            $ID = $contact->posts[0];
            $out['contact'] = array(
                'title'     => 'Contact',
                'id'        => $ID,
                'url'       => get_the_permalink($ID),
            );
        }
 
        wp_reset_postdata();
        $more  = new WP_Query(array(
            'post_type'          => 'page',
            'post_parent'        => $out['contact'],
            'posts_per_page'    => -1
        ));
        if ($more->have_posts()) {
            while ($more->have_posts()) {
                $more->the_post();
                $title = get_the_title();
                $out[sanitize_title($title)] = array(
                    'title' => $title,
                    'id'    => get_the_ID(),
                    'url'   => get_the_permalink(),
                );
            }
        }
        wp_reset_postdata();
 
        update_option('jvbContactPage', $out);
    }
    if (array_key_exists($page, $out)) {
        return $out[$page][$field];
    }
 
    return $out['contact']['url'];
}
 
 
/**
 * @return int|false
 */
function jvbAboutPage():int|false
{
    $out = get_option('jvbAboutPage');
    if (!$out) {
        $about = new WP_Query(array(
            'post_type'     => 'page',
            'title'         => 'About',
            'fields'        => 'ids'
        ));
        if ($about->have_posts()) {
            $out = $about->posts[0];
        }
    }
    return $out;
}
 
function jvbGetPageId(string $title):int
{
    $page = new WP_Query(
        [
            'post_type' => 'page',
            'title' => $title,
            'posts_per_page'    => 1,
            'fields'    => 'ids',
        ]
    );
    wp_reset_postdata();
    $result = ($page->have_posts()) ? $page->posts[0] : false;
    if (!$result) {
        $result = wp_insert_post([
            'post_title'    => $title,
            'post_type'     => 'page',
            'post_status'   => 'publish',
        ]);
    }
    return $result;
}