Skip to content

Instantly share code, notes, and snippets.

@seebz
Created July 16, 2018 18:02
Show Gist options
  • Save seebz/54732f446d17a511b098e24ed67cafcb to your computer and use it in GitHub Desktop.
Save seebz/54732f446d17a511b098e24ed67cafcb to your computer and use it in GitHub Desktop.
Test CPT WP avec recherche
<?php
/*
Plugin Name: Fruits
Description: J'aime les fruits.
Version: 1.0
Author: Seebz
*/
/**
* Enregistre notre CPT `fruit`.
*
* Éventuellement aller à l'adresse `/wp-admin/options-permalink.php` pour purger le cache `WP_Rewrite`
*/
function test_register_post_type() {
$post_type = 'fruit';
$args = array(
'labels' => array(
'name' => _x( 'Fruits', 'post type general name' ),
'singular_name' => _x( 'Fruit', 'post type singular name' ),
),
'public' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-post',
'hierarchical' => false,
'query_var' => false,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
);
// Permet d'avoir une page listant les fruits à l'adresse `/fruits/` (template `archive-fruit.php` si existant)
// La fonction `get_post_type_archive_link( 'fruit' )` devrait retourner l'url de cette page
//
// Autorise les recherches suivantes :
// - `/fruits/?s=banane`
// - '/?post_type=fruit&s=banane'
//
// https://codex.wordpress.org/Function_Reference/register_post_type#has_archive
$args['has_archive'] = 'fruits';
// Permet d'avoir une url du type `/fruits/banane/` au lieu de `/?post_type=fruit&p=42`
//
// https://codex.wordpress.org/Function_Reference/register_post_type#rewrite
$args['rewrite'] = array( 'slug' => 'fruits' );
return register_post_type( $post_type, $args );
}
add_action( 'init', 'test_register_post_type' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment