Skip to content

Instantly share code, notes, and snippets.

@bor0 bor0/41010.php
Created Jun 15, 2017

Embed
What would you like to do?
<?php
class TestClass {
private $post_title = 'Test Musicgenre Bookgenre';
private $taxonomies = array(
'bookgenre',
'musicgenre',
);
private $terms = array(
'bookgenre' => array(
'classic',
),
'musicgenre' => array(
'classic',
),
);
public function __construct() {
add_action( 'init', array( $this, 'register_taxonomies' ) );
add_action( 'init', array( $this, 'make_post' ), 11 );
add_action( 'init', array( $this, 'query_taxonomies' ), 12 );
}
public function register_taxonomies() {
foreach ( $this->taxonomies as $taxonomy ) {
register_taxonomy(
$taxonomy,
$taxonomy,
array(
'label' => __( $taxonomy ),
'rewrite' => array( 'slug' => $taxonomy ),
'hierarchical' => true,
'args' => [1, 2, 3],
)
);
}
foreach ( $this->terms as $taxonomy => $tax_terms ) {
foreach ( $tax_terms as $term ) {
if ( is_null( term_exists( $term, $taxonomy ) ) ) {
wp_insert_term(
$term,
$taxonomy,
array(
'description' => $term,
'slug' => $term,
)
);
}
}
}
}
public function get_test_post_id() {
global $wpdb;
return $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '$this->post_title'" );
}
public function make_post() {
global $wpdb;
if ( ! is_null( $this->get_test_post_id() ) ) {
return;
}
$my_post = array(
'post_title' => $this->post_title,
'post_content' => 'Test',
'post_status' => 'publish',
'post_author' => 1,
);
$post_id = wp_insert_post( $my_post );
wp_set_object_terms( $post_id, 'classic', 'musicgenre' );
wp_set_object_terms( $post_id, 'classic', 'bookgenre' );
}
public function query_taxonomies() {
$test = wp_get_object_terms([ $this->get_test_post_id() ], [ 'musicgenre', 'bookgenre' ]);
var_dump( $test );
}
}
new TestClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.