WordPress hwk Blog Gérer Dynamiquement les Queries & Templates des Post Types / Taxonomies

Gérer Dynamiquement les Queries & Templates des Post Types / Taxonomies

10 May 2018

Post Types

Exemple d’utilisation

<?php
add_action('init', 'hwk_post_type_example_register');
function hwk_post_type_example_register(){
register_post_type('example', array(
'label' => __('Example'),
'has_archive' => __('examples'),
'rewrite' => true,
'public' => true
));
}
// Args: Remove Single View
add_filter('hwk/post_type/example/args/no_single', '__return_true');
// Query: Post All
add_filter('hwk/post_type/example/query/post/all', 'hwk_post_type_example_post_all', 10, 2);
function hwk_post_type_example_post_all($post, $query){
$post->my_terms = false;
if(($terms = get_the_terms($post->ID, 'my_taxonomy')) && !is_wp_error($terms))
$post->my_terms = $terms;
return $post;
}
// Query: Post Single
add_filter('hwk/post_type/example/query/post/single', 'hwk_post_type_example_post_single', 10, 2);
function hwk_post_type_example_post_single($post, $query){
$post->my_meta = false;
if($meta = get_post_meta($post->ID, 'my_meta_key', true))
$post->my_meta = $meta;
return $post;
}
// Query: Archive
add_filter('hwk/post_type/example/query/archive', 'hwk_post_type_example_query_archive');
function hwk_post_type_example_query_archive($query){
$query->set('posts_per_page', 1);
$query->set('order', 'ASC');
return $query;
}
// Query: Single
add_filter('hwk/post_type/example/query/single', 'hwk_post_type_example_query_single');
function hwk_post_type_example_query_single($query){
return $query;
}
// Template: Archive
add_filter('hwk/post_type/example/template/archive', 'hwk_post_type_example_template_archive');
function hwk_post_type_example_template_archive(){
return 'templates/example/archive.php';
}
// Template: Single
add_filter('hwk/post_type/example/template/single', 'hwk_post_type_example_template_single');
function hwk_post_type_example_template_single(){
return 'templates/example/single.php';
}

Script

<?php
// Query
add_action('pre_get_posts', 'hwk_dynamic_pre_get_posts');
function hwk_dynamic_pre_get_posts($query){
if(is_admin() || !$query->is_main_query())
return;
// Post Types
if(is_home() || $query->is_post_type_archive() || is_singular()){
if(!$post_type = (is_home()) ? 'post' : $query->get('post_type'))
return;
$post_type_object = get_post_type_object($post_type);
$rule = array();
$rule['is_archive'] = is_post_type_archive($post_type_object->name);
$rule['has_archive'] = $post_type_object->has_archive;
if($post_type == 'post'){
$rule['is_archive'] = is_home();
$rule['has_archive'] = true;
}
// Single
if(is_singular($post_type))
$query = apply_filters('hwk/post_type/' . $post_type . '/query/single', $query);
// Archive
elseif($rule['has_archive'] && $rule['is_archive'])
$query = apply_filters('hwk/post_type/' . $post_type . '/query/archive', $query);
}
// Taxonomies
elseif($query->is_tax() || $query->is_category() || $query->is_tag()){
$queried_object = get_queried_object();
if(!$queried_object || !isset($queried_object->taxonomy))
return;
$query = apply_filters('hwk/taxonomy/' . $queried_object->taxonomy . '/query', $query);
}
}
// Template
add_filter('template_include', 'hwk_dynamic_template', 999);
function hwk_dynamic_template($template){
// Post Types
if(is_singular() || is_post_type_archive() || is_home()){
global $wp_query;
$post_type = get_post_type();
if(!$post_type && is_home())
$post_type = 'post';
if(!$post_type && isset($wp_query->query['post_type']))
$post_type = $wp_query->query['post_type'];
if(!$post_type)
return $template;
$post_type_object = get_post_type_object($post_type);
$rule = array();
$rule['is_archive'] = is_post_type_archive($post_type_object->name);
$rule['has_archive'] = $post_type_object->has_archive;
if($post_type == 'post'){
$rule['is_archive'] = is_home();
$rule['has_archive'] = true;
}
// Single
if(is_singular($post_type) && ($locate = locate_template(array(apply_filters('hwk/post_type/' . $post_type . '/template/single', $template)))))
return $locate;
// Archive
elseif($rule['has_archive'] && $rule['is_archive'] && ($locate = locate_template(array(apply_filters('hwk/post_type/' . $post_type . '/template/archive', $template)))))
return $locate;
return $template;
}
// Taxonomies
elseif(is_tax() || is_category() || is_tag()){
$queried_object = get_queried_object();
if(!$queried_object || !isset($queried_object->taxonomy))
return $template;
if(($locate = locate_template(array(apply_filters('hwk/taxonomy/' . $queried_object->taxonomy . '/template', $template)))))
return $locate;
return $template;
}
return $template;
}
// Post Type: Permalink
add_filter('post_type_link', 'hwk_dynamic_post_type_permalink', 10, 2);
add_filter('post_link', 'hwk_dynamic_post_type_permalink', 10, 2);
function hwk_dynamic_post_type_permalink($link, $post){
if(!apply_filters('hwk/post_type/' . $post->post_type . '/args/no_single', false))
return $link;
if($post_type_archive_link = get_post_type_archive_link($post->post_type))
return $post_type_archive_link;
return home_url();
}
// Post Type: Rewrite Rules
add_action('init', 'hwk_dynamic_post_type_rewrite_rules', 999);
function hwk_dynamic_post_type_rewrite_rules(){
$post_types = get_post_types(array('publicly_queryable' => true), 'objects');
if(empty($post_types))
return;
foreach($post_types as $post_type){
if(!apply_filters('hwk/post_type/' . $post_type->name . '/args/no_single', false))
continue;
add_filter($post_type->name . '_rewrite_rules', '__return_empty_array');
}
}
// Post Type: Row Actions
add_filter('page_row_actions', 'hwk_dynamic_post_type_row_actions', 10, 2);
add_filter('post_row_actions', 'hwk_dynamic_post_type_row_actions', 10, 2);
function hwk_dynamic_post_type_row_actions($actions, $post){
if(!apply_filters('hwk/post_type/' . $post->post_type . '/args/no_single', false))
return $actions;
unset($actions['view']);
return $actions;
}
// Post Type: Args
add_filter('register_post_type_args', 'hwk_dynamic_post_type_args', 10, 2);
function hwk_dynamic_post_type_args($args, $post_type){
if(!apply_filters('hwk/post_type/' . $post_type . '/args/no_single', false))
return $args;
if($args['show_ui'] === null)
$args['show_ui'] = true;
if($args['show_in_nav_menus'] === null)
$args['show_in_nav_menus'] = true;
if($args['publicly_queryable'] === null)
$args['publicly_queryable'] = true;
if($args['exclude_from_search'] === null)
$args['exclude_from_search'] = true;
$args['public'] = false;
return $args;
}
// WP_Post
add_filter('posts_results', 'hwk_dynamic_post_type_wp_post', 10, 2);
function hwk_dynamic_post_type_wp_post($posts, $query){
if(empty($posts))
return $posts;
foreach($posts as $post){
$post = apply_filters('hwk/post_type/' . get_post_type($post) . '/query/post/all', $post, $query);
if($query->is_singlular())
$post = apply_filters('hwk/post_type/' . get_post_type($post) . '/query/post/single', $post, $query);
}
return $posts;
}

Taxonomies

Exemple d’utilisation

<?php
// Taxonomy
add_action('init', 'hwk_taxonomy_tax_example_register');
function hwk_taxonomy_tax_example_register() {
register_taxonomy('tax_example', array('example'), array(
'label' => __('Tax Example'),
'hierarchical' => false,
'public' => true
));
}
// Args: Remove Single View
add_filter('hwk/taxonomy/tax_example/args/no_single', '__return_true');
// Query
add_filter('hwk/taxonomy/tax_example/query', 'hwk_taxonomy_tax_example_query');
function hwk_taxonomy_tax_example_query($query){
$query->set('posts_per_page', 1);
$query->set('order', 'ASC');
return $query;
}
// Template
add_filter('hwk/taxonomy/tax_example/template', 'hwk_taxonomy_tax_example_template');
function hwk_taxonomy_tax_example_template(){
return 'templates/tax_example/archive.php';
}

Script

<?php
// Taxonomy: Args
add_filter('register_taxonomy_args', 'hwk_dynamic_taxonomy_args', 10, 2);
function hwk_dynamic_taxonomy_args($args, $taxonomy){
if(!apply_filters('hwk/taxonomy/' . $taxonomy . '/args/no_single', false))
return $args;
if($args['show_ui'] === null)
$args['show_ui'] = true;
if($args['show_in_nav_menus'] === null)
$args['show_in_nav_menus'] = true;
if($args['publicly_queryable'] === null)
$args['publicly_queryable'] = true;
if($args['exclude_from_search'] === null)
$args['exclude_from_search'] = true;
$args['public'] = false;
$args['rewrite'] = false;
return $args;
}
// Taxonomy: Permalink
add_filter('term_link', 'hwk_dynamic_taxonomy_permalink', 10, 3);
function hwk_dynamic_taxonomy_permalink($link, $term, $taxonomy){
if(!apply_filters('hwk/taxonomy/' . $taxonomy . '/args/no_single', false))
return $link;
return home_url();
}


Konrad Chmielewski

Evangeliste WordPress & Full Stack Developer depuis 10 années.