php 如何将自定义过滤器字段添加到没有过滤器插件的Tribe事件日历(在此示例中 - 按类别和标记过滤)。示例:https:

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 如何将自定义过滤器字段添加到没有过滤器插件的Tribe事件日历(在此示例中 - 按类别和标记过滤)。示例:https:相关的知识,希望对你有一定的参考价值。

<?php

// Example: https://monosnap.com/file/kD9Liz759zrsxi6lnSL49KHxAu8haX
// Tested with Tribe Events Calendar 4.6.12

/**
 * Set up the keyword search in the tribe events bar.
 *
 * @param array $filters The current filters in the bar array.
 *
 * @return array The modified filters array.
 */
add_filter( 'tribe-events-bar-filters', function($filters){
    $selected_category = '';
    if ( ! empty( $_REQUEST['tribe-category-search'] ) ) {
        $category = sanitize_title ($_REQUEST['tribe-category-search'] );
    } 
    
    $selected_tag = '';
    if ( ! empty( $_REQUEST['tribe-tag-search'] ) ) {
        $selected_tag = sanitize_title ($_REQUEST['tribe-tag-search'] );
    }

    $event_categories = calendar_get_categories_list();
    $event_tags = calendar_get_tags_list();

    $event_categories_html = sprintf('<option value="">%s</option>', 'Select Category');
    foreach ($event_categories as $event_tax) {
        $event_categories_html .= sprintf('<option value="%2$s" %3$s>%1$s</option>',
            $event_tax->name, $event_tax->slug, selected($event_tax->slug,$selected_category, false ));
    }
    
    $event_tags_html = sprintf('<option value="">%s</option>', 'Select Tag');
    foreach ($event_tags as $event_tax) {
        $event_tags_html .= sprintf('<option value="%2$s" %3$s>%1$s</option>',
            $event_tax->name, $event_tax->slug, selected($event_tax->slug,$selected_tag, false ));
    }

    $filters['tribe-tag-search'] = array(
        'name'    => 'tribe-tag-search',
        'caption' => esc_html__( 'Category', 'the-events-calendar' ),
        'html'    => '<select type="text" name="tribe-tag-search" id="tribe-tag-search">' . $locations_options_html . '</select>',
    );

    $filters['tribe-category-search'] = array(
        'name'    => 'tribe-category-search',
        'caption' => esc_html__( 'Tags', 'the-events-calendar' ),
        'html'    => '<select type="text" name="tribe-category-search" id="tribe-category-search">' . $event_tags_html . '</select>',
    );

    return $filters_new;
}, 11, 1 );


function calendar_get_tags_list() {

    $r = wp_cache_get("calendar_get_tags_list");

    if ( $r ) {
        return $r;
    }


    $all_tags = get_terms( array('taxonomy' => 'post_tag', 'hide_empty'=>true) );

    $tags_by_SLUG = array();

    foreach ($all_tags as $terms_ID => $term) {
        $tags_by_SLUG[$term->slug] = $term->name;
    }

    wp_cache_add("calendar_get_tags_list", $tags_by_SLUG);

    return $tags_by_SLUG;
}

function calendar_get_categories_list() {

    $r = wp_cache_get("calendar_get_categories_list");

    if ( $r ) {
        return $r;
    }


    $all_tags = get_terms( array('taxonomy' => Tribe__Events__Main::TAXONOMY, 'hide_empty'=>true) );

    $tags_by_SLUG = array();

    foreach ($all_tags as $terms_ID => $term) {
        $tags_by_SLUG[$term->slug] = $term->name;
    }

    wp_cache_add("calendar_get_categories_list", $tags_by_SLUG);

    return $tags_by_SLUG;
}

/**
 * Filter posts by our Category or Tag slug
*/
add_filter('pre_get_posts', function($query) {
    
    /** @var WP_Query $query */

    if ( !empty( $_REQUEST['tribe-category-search'] ) ) {
        $category_slug = sanitize_title( $_REQUEST['tribe-category-search'] );

        $tax_query[] = array(
            'taxonomy'         => Tribe__Events__Main::TAXONOMY,
            'field'            => 'slug',
            'terms'            => [$category_slug],
            'include_children' => false,
            'operator'         => 'IN',
        );

        $query->set( 'tax_query', $tax_query );
    }

    if ( !empty( $_REQUEST['tribe-tag-search'] ) ) {
        $location_slug = sanitize_title( $_REQUEST['tribe-tag-search'] );

        $tax_query[] = array(
            'taxonomy'         => 'post_tag',
            'field'            => 'slug',
            'terms'            => [$location_slug],
            'include_children' => false,
            'operator'         => 'IN',
        );

        $query->set( 'tax_query', $tax_query );
    }


    return $query;
}, 55 );

以上是关于php 如何将自定义过滤器字段添加到没有过滤器插件的Tribe事件日历(在此示例中 - 按类别和标记过滤)。示例:https:的主要内容,如果未能解决你的问题,请参考以下文章

如何将自定义标签/过滤器添加到现有的 Django 应用程序?

如何将自定义授权过滤器添加到ASP网络核心中非控制器的方法?

如何将自定义挂钩添加到 Woocommerce 的自定义插件

如何将自定义对象从异步操作过滤器传递到 ASP.net 核心中的控制器?

php 将自定义字段内容添加到菜单项

php 将自定义字段添加到购物车/结帐