-1

I am working on the following code. The issue is as follows:

Custom Post-Type Page to use URL:

security/ssl-certificates/%ssl_brand%/[POST-TYPE-ITEM]

Custom Taxonomy I need to be accessible via URL:

security/ssl-certificates/[TAXONOMY]

I have been playing around but cannot seem to crack it for POST-TYPE, when I see the view links for the brands in the custom taxonomy they show as;

/security/ssl-certificates/geotrust (geotrust being the taxonomy brand)

so that seems to be fine but cannot get it working on the Post-Type URL, tried adding %ssl_brand% but shows as this;

security/ssl-certificates/%ssl_brand%/post-type-item-slug

See in the code below:

functions.php

function cptui_register_my_cpts_ssl_certificates() {

    /**
     * Post Type: SSL Certificates.
     */

    $labels = array(
        "name" => __( "SSL Certificates", "foxhost-child" ),
        "singular_name" => __( "SSL Certificate", "foxhost-child" ),
        "all_items" => __( "SSL Certificates", "foxhost-child" ),
        "add_new" => __( "Add New SSL", "foxhost-child" ),
        "add_new_item" => __( "Add New SSL", "foxhost-child" ),
        "edit_item" => __( "Edit SSL", "foxhost-child" ),
        "new_item" => __( "New SSL", "foxhost-child" ),
        "view_item" => __( "View SSL", "foxhost-child" ),
        "view_items" => __( "View All SSL's", "foxhost-child" ),
        "search_items" => __( "Search SSL", "foxhost-child" ),
        "not_found" => __( "No SSL's Found", "foxhost-child" ),
        "not_found_in_trash" => __( "No SSL's found in Trash", "foxhost-child" ),
        "parent_item_colon" => __( "security,products", "foxhost-child" ),
        "parent_item_colon" => __( "security,products", "foxhost-child" ),
    );

    $args = array(
        "label" => __( "SSL Certificates", "foxhost-child" ),
        "labels" => $labels,
        "description" => "SSL Certificate Products",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => false,
        "show_in_menu" => "edit.php?post_type=products",
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "security/ssl-certificates/%ssl_brand%", "with_front" => false ),
        "query_var" => "ssl_brand",
        "menu_icon" => "dashicons-lock",
        "supports" => array( "title", "editor", "revisions" ),
        "taxonomies" => array( "ssl_brand" ),
    );

    register_post_type( "ssl_certificates", $args );
}

add_action( 'init', 'cptui_register_my_cpts_ssl_certificates' );




function cptui_register_my_taxes_ssl_brand() {

    /**
     * Taxonomy: SSL Brands.
     */

    $labels = array(
        "name" => __( "SSL Brands", "foxhost-child" ),
        "singular_name" => __( "Brand", "foxhost-child" ),
    );

    $args = array(
        "label" => __( "SSL Brands", "foxhost-child" ),
        "labels" => $labels,
        "public" => true,
        "hierarchical" => false,
        "label" => "SSL Brands",
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'security/ssl-certificates', 'with_front' => false, ),
        "show_admin_column" => true,
        "show_in_rest" => false,
        "rest_base" => "ssl_brand",
        "show_in_quick_edit" => false,
    );
    register_taxonomy( "ssl_brand", array( "ssl_certificates" ), $args );
}

add_action( 'init', 'cptui_register_my_taxes_ssl_brand' );
halfer
  • 18,701
  • 13
  • 79
  • 158
James
  • 1,561
  • 14
  • 38

1 Answers1

0

I worked it out myself with this function addition (for anyone that needs a similar solution):

function build_ssl_url_with_brand( $post_link, $id = 0, $leavename = FALSE ) {
  if ( strpos('%ssl_brand%', $post_link) === 'FALSE' ) {
    return $post_link;
  }
  $post = get_post($id);
  if ( !is_object($post) || $post->post_type != 'ssl_certificates' ) {
    return $post_link;
  }
  $terms = wp_get_object_terms($post->ID, 'ssl_brand');
  if ( !$terms ) {
    return str_replace('security/ssl-certificates/%ssl_brand%/', '', $post_link);
  }
  return str_replace('%ssl_brand%', $terms[0]->slug, $post_link);
}
add_filter('post_type_link', 'build_ssl_url_with_brand', 10, 2);
halfer
  • 18,701
  • 13
  • 79
  • 158
James
  • 1,561
  • 14
  • 38