PHP code example of hard-g / cpt-tax

1. Go to this page and download the library: Download hard-g/cpt-tax library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hard-g / cpt-tax example snippets


\HardG\CptTax\Registry::register( 'actors', 'actor_pt', 'actor_tax' );

// Fetch the term ID corresponding to the Actor post.
$actor_term_id = \HardG\CptTax\Registry::get_term_id_for_post_id( 'actors', $actor_post_id );

wp_set_object_terms( $movie_post_id, [ $actor_term_id ], 'actor_tax', true );

// Fetch the term ID corresponding to the Actor post.
$actor_term_id = \HardG\CptTax\Registry::get_term_id_for_post_id( 'actors', $actor_post_id );

$movies_with_actor = get_posts(
  [
    'post_type' => 'movie_pt',
    'tax_query' => [
      [
        'taxonomy' => 'actor_tax',
	'terms'    => $actor_term_id,
      ]
    ],
  ]
);

$actor_posts = array_map(
  function( $actor_term ) {
    $actor_post_id = \HardG\CptTax\Registry::get_post_id_for_term_id( 'actors', $actor_term->term_id );
    return get_post( $actor_post_id );
  },
  wp_get_object_terms( $movie_post_id, 'actor_tax' );
);

register_taxonomy(
  'my_taxonomy',
  $my_post_types,
  [
    // ...
    'public' => false,
    'show_ui' => false, // Unless you want to be able to see it for diagnostic purposes in the Dashboard
    'show_in_rest' => false,
    // ...
  ]
);

// ...
'capabilities' => [
  'manage_terms' => 'do_not_allow',
  'edit_terms'   => 'do_not_allow',
  'delete_terms' => 'do_not_allow',
],
// ...