catthr
1/13/2015 - 6:28 AM

Create post type in wordpress with taxonomy https://developer.wordpress.org/resource/dashicons/

Create post type in wordpress with taxonomy https://developer.wordpress.org/resource/dashicons/

// Управление списком записей произвольного типа. 
// В вордпрессе есть несколько встроенных типов, таких как автор, название, теги. Автор по умоланию не выводится.
// Используется manage_$post_type_posts_columns фильтр
add_filter('manage_book_posts_columns' , 'add_author_column');
function add_author_column($columns){
        $columns['author'] = 'Клиент';
        return $columns;
    }
    
// Можно добавлять в список произвольные столбцы, тогда в другом методе нужно определить какое значение там указывать.
// Используется manage_$post_type_posts_custom_column
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function custom_book_column( $column, $post_id ) {
    switch ( $column ) {

        case 'book_author' :
            $terms = get_the_term_list( $post_id , 'book_author' , '' , ',' , '' );
            if ( is_string( $terms ) )
                echo $terms;
            else
                _e( 'Unable to get author(s)', 'your_text_domain' );
            break;

        case 'publisher' :
            echo get_post_meta( $post_id , 'publisher' , true ); 
            break;

    }
}
<?php

add_action( 'init', 'motiv_create_posttype', 0 );
function motiv_create_posttype() {
    register_post_type( 'motiv_video',
        array(
            'labels' => array(
                'name' => __( 'Видео' ),
                'singular_name' => __( 'Видео' )
            ),
            'public' => false,
            'has_archive' => false,
            'publicly_queryable' => false,
            'menu_icon' => 'dashicons-video-alt3',
            'show_in_menu'  => true,
            'show_ui'       => true,
        )
    );
}

?>
add_action( 'init', 'create_portfolio_posttype', 0 );
function create_portfolio_posttype() {
            register_post_type( 'pl_portfolio',
                array(
                    'labels' => array(
                        'name' => __( 'Портфолио' ),
                        'singular_name' => __( 'Портфолио' )
                    ),
                    'public' => true,
                    'has_archive' => false,
                    'publicly_queryable' => true,
                    'menu_icon' => 'dashicons-images-alt2',
                    'show_in_menu'  => true,
                    'show_ui'       => true,
                    'rewrite' => array(
                        'slug' => 'portfolio'
                    )
                )
            );
        }
<?php

add_action( 'init', 'motiv_create_taxonomy', 0 );
function motiv_create_taxonomy() {
    $labels = array(
        'name' => _x( 'Тип', 'taxonomy general name' ),
        'singular_name' => _x( 'Тип', 'taxonomy singular name' ),
        'search_items' =>  __( 'Тип' ),
        'popular_items' => __( 'Тип' ),
        'all_items' => __( 'Тип' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Поменять тип' ),
        'update_item' => __( 'Обновить тип' ),
        'add_new_item' => __( 'Добавить новый тип' ),
        'new_item_name' => __( 'Новый тип' ),
        'separate_items_with_commas' => __( 'Тип' ),
        'add_or_remove_items' => __( 'Тип' ),
        'choose_from_most_used' => __( 'Тип' ),
        'menu_name' => __( 'Тип' ),
    );
    register_taxonomy('motiv_display','motiv_video',array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
    ));
}
add_action('restrict_manage_posts', 'restrict_books_by_genre');
function restrict_books_by_genre() {
		global $typenow;
		$post_type = 'books'; // change HERE
		$taxonomy = 'genre'; // change HERE
		if ($typenow == $post_type) {
			$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
			$info_taxonomy = get_taxonomy($taxonomy);
			wp_dropdown_categories(array(
				'show_option_all' => __("Show All {$info_taxonomy->label}"),
				'taxonomy' => $taxonomy,
				'name' => $taxonomy,
				'orderby' => 'name',
				'selected' => $selected,
				'show_count' => true,
				'hide_empty' => true,
			));
		};
	}

	add_filter('parse_query', 'convert_id_to_term_in_query');
	function convert_id_to_term_in_query($query) {
		global $pagenow;
		$post_type = 'books'; // change HERE
		$taxonomy = 'genre'; // change HERE
		$q_vars = &$query->query_vars;
		if ($pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0) {
			$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
			$q_vars[$taxonomy] = $term->slug;
		}
	}
/* categories */
<?php
          $cat_args = array( 'taxonomy' => 'pl_portfolio_category' );
          $categories = get_categories( $cat_args );
          foreach ( $categories as $category ) { ?>
              <li data-category="<?= $category->slug ?>"><?= $category->name ?></li>
          <?php }?>
          
         <?php
              $args = array( 'post_type' => 'pl_portfolio', 'posts_per_page' => -1 );
              $the_query = new WP_Query( $args );
              if ( $the_query->have_posts() ) : ?>
                          <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                              <?php global $post;
                              $category = wp_get_post_terms( $post->ID, 'pl_portfolio_category' );
                              $category = $category[0]; ?>

                              <li data-category="<?= $category->slug ?>" >
                                  <a href="<?= get_permalink() ?>">
                                      <img src="<?= get_field('картинка_в_списке') ?>" alt=""/>
                                      <p>
                                          <span class="title"><?= get_field('заголовок_в_списке') ?></span>
                                          <span class="date"><?= get_field('дата') ?></span>
                                      </p>
                                  </a>
                              </li>
                          <?php endwhile; ?>
                          <?php wp_reset_postdata(); ?>
              <?php endif;
          ?>
<?php
/*
Plugin Name: -
Plugin URI: -
Description: Theme Plugin
Version: 0.1
Author: Catherine
*/

defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );

if ( ! class_exists( 'Bartending' ) ) {
	class Bartending
	{
		public function __construct()
		{
            add_action( 'init', array( &$this, 'create_portfolio_posttype' ), 0 );
            add_action( 'init', array( &$this, 'create_portfolio_taxonomy' ), 0 );
            add_action( 'restrict_manage_posts', array( &$this, 'restrict_portfolio_by_category' ) );
            add_filter( 'parse_query', array( &$this, 'convert_id_to_term_in_query' ) );

		}

        function create_portfolio_posttype() {
            register_post_type( 'pl_portfolio',
                array(
                    'labels' => array(
                        'name' => __( 'Портфолио' ),
                        'singular_name' => __( 'Портфолио' )
                    ),
                    'public' => false,
                    'has_archive' => false,
                    'publicly_queryable' => false,
                    'menu_icon' => 'dashicons-video-alt3',
                    'show_in_menu'  => true,
                    'show_ui'       => true,
                )
            );
        }

        function create_portfolio_taxonomy() {
            $labels = array(
                'name' => _x( 'Тип', 'taxonomy general name' ),
                'singular_name' => _x( 'Категория', 'taxonomy singular name' ),
                'search_items' =>  __( 'Категория' ),
                'popular_items' => __( 'Категория' ),
                'all_items' => __( 'Категории' ),
                'parent_item' => null,
                'parent_item_colon' => null,
                'edit_item' => __( 'Поменять категорию' ),
                'update_item' => __( 'Обновить категорию' ),
                'add_new_item' => __( 'Добавить категорию' ),
                'new_item_name' => __( 'Новая категория' ),
                'separate_items_with_commas' => __( 'Категория' ),
                'add_or_remove_items' => __( 'Категория' ),
                'choose_from_most_used' => __( 'Категория' ),
                'menu_name' => __( 'Категории' ),
            );
            register_taxonomy('pl_portfolio_category','pl_portfolio',array(
                'hierarchical' => true,
                'labels' => $labels,
                'show_ui' => true,
                'show_admin_column' => true,
                'update_count_callback' => '_update_post_term_count',
                'query_var' => true,
            ));
        }

        function restrict_portfolio_by_category() {
            global $typenow;
            $post_type = 'pl_portfolio';
            $taxonomy = 'pl_portfolio_category';
            if ($typenow == $post_type) {
                $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
                $info_taxonomy = get_taxonomy($taxonomy);
                wp_dropdown_categories(array(
                    'show_option_all' => __("Show All {$info_taxonomy->label}"),
                    'taxonomy' => $taxonomy,
                    'name' => $taxonomy,
                    'orderby' => 'name',
                    'selected' => $selected,
                    'show_count' => true,
                    'hide_empty' => true,
                ));
            };
        }

        function convert_id_to_term_in_query($query) {
            global $pagenow;
            $post_type = 'pl_portfolio';
            $taxonomy = 'pl_portfolio_category';
            $q_vars = &$query->query_vars;
            if ($pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0) {
                $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
                $q_vars[$taxonomy] = $term->slug;
            }
        }

	}
	new Bartending;
}