0

does someone could point me a lecture that could explain me how to create some "filter" passed by pretty link ?

Let me explain my self, I have a custom post type called "spectacle" and I have a meta_key who contain an array of dates for this spectacle. I know how to extract my date, and filter them if I give explicitly some filter to my display, my problem is not coming from there, what I'm trying to achieve, is to access

http://myurl.com/spectacles (show upcoming spectacles - already done) http://myurl.com/spectacles/2014 (show all 2014 spectacle) http://myurl.com/spectacles/2014/01 (show all spectacle from january 2014) http://myurl.com/spectacles/2014/01/01 (show all spectacle from january 1st 2014) http://myurl.com/spectacle/name-of-the-spectacle (show this spectacle - already done)

All i need to is a way to enable theses permalink and to extract theses parameters than i'll be able to do what I want, I already have my filtering functions.

This is what i've done so far :

// Register Custom Post Type
function build_cpt() {
$labels  = array(
    'name'               => _x( 'Programmation', 'Post Type General Name', 'mlog_lang' ),
    'singular_name'      => _x( 'Spectacles', 'Post Type Singular Name', 'mlog_lang' ),
    'menu_name'          => __( 'Programmation', 'mlog_lang' ),
    'all_items'          => __( 'Tous les spectacles', 'mlog_lang' ),
    'view_item'          => __( 'Afficher ce spectacle', 'mlog_lang' ),
    'add_new_item'       => __( 'Ajouter un spectacle', 'mlog_lang' ),
    'add_new'            => __( 'Ajouter un spectacle', 'mlog_lang' ),
    'edit_item'          => __( 'Modifier un spectacle', 'mlog_lang' ),
    'update_item'        => __( 'Mettre à jour ce spectacle', 'mlog_lang' ),
    'search_items'       => __( 'Rechercher un spectacle', 'mlog_lang' ),
    'not_found'          => __( 'Aucun spectacle trouvé', 'mlog_lang' ),
    'not_found_in_trash' => __( 'Aucun spectacle dans la corbeille', 'mlog_lang' ),
);

add_rewrite_tag( '%event-year%', '(\d{4})' );
//add_rewrite_tag( '%event-month%', '(\d{2})' );
//add_rewrite_tag( '%event-day%', '(\d{2}|\d{1})' );

$rewrite = array(
    //'slug'       => 'spectacles/%event-year%/%event-month%/%event-day%/',
    'slug'       => 'spectacles/%event-year%/',
    'with_front' => true,
    'pages'      => true,
    'feeds'      => true,
);
$args    = array(
    'label'               => __( 'programmation', 'mlog_lang' ),
    'description'         => __( 'Liste des spectacles', 'mlog_lang' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'revisions' ),
    'taxonomies'          => array(),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => false,
    'show_in_admin_bar'   => false,
    'menu_position'       => 4,
    'menu_icon'           => 'dashicons-groups',
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'rewrite'             => $rewrite,
    'capability_type'     => 'page',
);
register_post_type( 'programmation', $args );

Now I can access :

http://myurl.com/spectacles/2014

but it shows the home page instead of archive-programmation.php

And I can't access :

http://myurl.com/spectacles/name-of-the-spectacle

3
  • you need to add rewrite rules, search this site for add_rewrite_rule for some examples.
    – Milo
    May 21, 2014 at 20:43
  • I tried that solution, but there's two problem to address, first when I had let say add_rewrite_tag( '%event-year%', '(\d{4})' ); I can access myurl.com/spectacle/2014 but I can't access myurl.com/spectacle AND the second problem is that myurl.com/spectacle/2014 show me the front-page instead of the archive-spectacle.php ! May 22, 2014 at 12:55
  • I said rule not tag.
    – Milo
    May 22, 2014 at 13:59

1 Answer 1

1

The good answer was from @Milo, but he haven't posted any code, so I post here my result, for people coming here for the right answer.

/* Add query var to authorized query var... */
add_filter( 'query_vars', 'add_spectacles_query_vars' );
function add_spectacles_query_vars( $query_vars ) {
    $query_vars[] = 'spectacles-year';
    $query_vars[] = 'spectacles-month';
    $query_vars[] = 'spectacles-day';
    return $query_vars;
}

/* Add rewrite rule */
add_action( 'init', 'add_spectacles_rewrite_rule' );
function add_spectacles_rewrite_rule()
{
    add_rewrite_rule(
        'spectacles\/(on){1}\/{1}(\d{4})+\/?(\d{2})?\/?(\d{2}|d{1})?',
        'index.php?post_type=programmation&spectacles-year=$matches[2]&spectacles-month=$matches[3]&spectacles-day=$matches[4]',
        'top'
    );
    //flush_rewrite_rules();
}

Than in my template I can go with

$spectacle_year = get_query_var('spectacles-year');
$spectacle_month = get_query_var('spectacles-month');
$spectacle_day = get_query_var('spectacles-day');

P.S. I'm filtering my spectacles directly in the archive page, but it could be done via :

add_action( 'pre_get_posts', 'spectacles_archvies_pre_get_posts', 11 );
function spectacles_archvies_pre_get_posts($query) {
    if( !empty($query->query_vars['spectacles-year']) ) {
        /* Do your stuff here... */
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.