Wordpress:自定义URL参数,自定义分类和自定义术语字段的cutom查询

时间:2016-07-01 10:50:08

标签: wordpress

我有自定义网址参数?external_slug=,自定义分类manufacturer和分类external_slug中的自定义字段。

自定义网址参数?external_slug=

function parameter_queryvars_a( $qvars )
{
    $qvars[] = 'taxonomy';
    $qvars[] = 'slug';
    $qvars[] = 'external_slug';
    return $qvars;
}
add_filter('query_vars', 'parameter_queryvars_a' );

自定义分类manufacturer

function create_manufacturer_taxonomy()
{
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Manufacturers', 'taxonomy general name' ),
        'singular_name'     => _x( 'Manufacturer', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Manufacturers' ),
        'all_items'         => __( 'All Manufacturers' ),
        'parent_item'       => __( 'Parent Manufacturer' ),
        'parent_item_colon' => __( 'Parent Manufacturer:' ),
        'edit_item'         => __( 'Edit Manufacturer' ),
        'update_item'       => __( 'Update Manufacturer' ),
        'add_new_item'      => __( 'Add New Manufacturer' ),
        'new_item_name'     => __( 'New Manufacturer Name' ),
        'menu_name'         => __( 'Manufacturer' ),
    );
    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'manufacturer' ),
    );
    register_taxonomy( 'manufacturer', array( 'product' ), $args );
}
add_action( 'init', 'create_manufacturer_taxonomy', 0 );

分类external_slug中的自定义字段:

function manufacturer_taxonomy_add_new_meta_fields($taxonomy)
{
    <div class="form-field">
        <label for="term_meta[external_slug]"><?php _e( 'External slug', 'manufacturer' ); ?></label>
        <input type="text" name="term_meta[external_slug]" id="term_meta[external_slug]" value="">
        <p class="description"><?php _e( 'Enter wheel-size.com slug','manufacturer' ); ?></p>
    </div>
    <?php
}
add_action( 'manufacturer_add_form_fields', 'manufacturer_taxonomy_add_new_meta_fields', 10, 2 );

// Edit term page
function manufacturer_taxonomy_edit_meta_field($term)
{ 
    // put the term ID into a variable
    $t_id = $term->term_id;
    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="term_meta[external_slug]"><?php _e( 'External slug', 'manufacturer' ); ?></label></th>
        <td>
            <input type="text" name="term_meta[external_slug]" id="term_meta[external_slug]" value="<?php echo esc_attr( $term_meta['external_slug'] ) ? esc_attr( $term_meta['external_slug'] ) : ''; ?>">
            <p class="description"><?php _e( 'Enter external slug','manufacturer' ); ?></p>
        </td>
    </tr>
    <?php
}
add_action( 'manufacturer_edit_form_fields', 'manufacturer_taxonomy_edit_meta_field', 10, 2 );

// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id )
{
    if ( isset( $_POST['term_meta'] ) )
    {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
    }
}  
add_action( 'edited_manufacturer', 'save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_manufacturer', 'save_taxonomy_custom_meta', 10, 2 );

现在的问题是,如何修改查询以获取具有以下参数的产品帖子: /?post_type=product&taxonomy=manufacturer&slug=abc&external_slug=aBc

分类=制造商

term [slug] = slug

term [external_slug] = external_slug

function modify_query_taxonomy_slug_external_slug($query)
{
    global $wp_query;
    if (isset($wp_query->query_vars['taxonomy']) && isset($wp_query->query_vars['slug']) && isset($wp_query->query_vars['external_slug']))
    {
        //???
    }
}
add_action( 'pre_get_posts', 'modify_query_taxonomy_slug_external_slug' );

0 个答案:

没有答案