CPT UI – A Weblog of Priyank Maniar https://priyank.rocks Articles on web development & mobile app development Thu, 16 Jul 2020 19:37:43 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 How to remove the custom post type name (CPT) from the URL/Slug? https://priyank.rocks/how-to-remove-the-custom-post-type-name-cpt-from-the-url-slug/ Thu, 16 Jul 2020 16:28:08 +0000 https://priyank.rocks/?p=6949 My usecase was I was having a link domain.com/tutors/tutor-name. The tutors is cpt and the tutor-name is the slug.

Desired output was domain.com/tutor-name.

The following code is an extract from the StackExchange but it contains a few comments that clarify a few things for me.

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
/**
 *
 * Source: https://wordpress.stackexchange.com/questions/203951/remove-slug-from-custom-post-type-post-urls
 *
 * Doesn't change the permalink (get_permalink(id)) for the cpt post but the expected link does work with just /slug instead of /tutors(cpt)/slug
 *
 * You can do the following to manipulate the permalink
 *
 * ```
 *
 *   'tutorProfileLink' => str_replace('/tutors/', '/', get_permalink(get_the_ID()))
 *
 * ```
 *
 * Please note the original link of /tutors(cpt)/slug will still be working as is and also /slug will work
 *
 * @param $post_link
 * @param $post
 * @param $leavename
 * @return mixed
 *
 */

function na_remove_slug($post_link, $post, $leavename)
{

    if ('events' != $post->post_type || 'publish' != $post->post_status) {
        return $post_link;
    }

    $post_link = str_replace('/' . $post->post_type . '/', '/', $post_link);

    return $post_link;

}

add_filter('post_type_link', 'na_remove_slug', 10, 3);

function na_parse_request($query)
{

    if (!$query->is_main_query() || 2 != count($query->query) || !isset($query->query['page'])) {
        return;
    }

    if (!empty($query->query['name'])) {
        $query->set('post_type', array('post', 'tutors', 'page'));
    }
}

add_action('pre_get_posts', 'na_parse_request');

I also kept the above settings. Not that it did any magic but just for having the log.

]]>
Adding a CPT as a child or nested CPT in the WP admin left sidebar https://priyank.rocks/adding-a-cpt-as-a-child-or-nested-cpt-in-the-wp-admin-left-sidebar/ Thu, 28 May 2020 09:54:45 +0000 https://priyank.rocks/?p=6694 Edit CPT UI – For the item show in the menu, type in following where event_management is a parent post type and the current editing post type becomes a child of it.

edit.php?post_type=event_management

]]>