Как отфильтровать пользовательские сообщения с тем же термином таксономии, что и текущее сообщение? - PullRequest
0 голосов
/ 12 сентября 2018

Я пытаюсь отфильтровать пользовательские сообщения с помощью моей собственной таксономии, называемой «событиями».У меня есть код, который должен проверить name таксономии «событий» для текущего сообщения, а затем перечислить только сообщения в той же группе.

Я проверил страницу кодекса https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters, но не смог заставить это работать.У кого-нибудь есть какие-либо советы?

$post_terms = wp_get_object_terms($post->ID, 'events');
$post_term = $post_terms[0]->name;

    $args = array(
        'post_type' => 'events',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'post__not_in' => array(124470, 124455),
        'tax_query' => array(
            array(
                'taxonomy' => 'events',
                'field'    => 'name',
                'terms'    => $post_term,


        ),
    ),
);

обновление, добавленный ниже код по запросу.

/* custom taxonomies */
function my_custom_events_taxonomies(){

    register_taxonomy(
        'events',
        'events',
        array(
            'label' => 'Events type',
            'rewrite' => array( 'slug' => 'Events'),
            'hierarchical' => false,
            'custom-fields' => true,
            )

    );
}

add_action ( 'init', 'my_custom_events_taxonomies');

пользовательский тип сообщения ниже:

function events_post_type() {

$labels = array(
    'name'                  => _x( 'Events', 'Post Type General Name', 'remco' ),
    'singular_name'         => _x( 'Events', 'Post Type Singular Name', 'remco' ),
    'menu_name'             => __( 'Events', 'remco' ),
    'name_admin_bar'        => __( 'Events', 'remco' ),
    'archives'              => __( 'Events Archives', 'remco' ),
    'parent_item_colon'     => __( 'Parent Events:', 'remco' ),
    'all_items'             => __( 'All Events', 'remco' ),
    'add_new_item'          => __( 'Add New Events', 'remco' ),
    'add_new'               => __( 'Add New', 'remco' ),
    'new_item'              => __( 'New Item', 'remco' ),
    'edit_item'             => __( 'Edit Item', 'remco' ),
    'update_item'           => __( 'Update Item', 'remco' ),
    'view_item'             => __( 'View Item', 'remco' ),
    'search_items'          => __( 'Search Item', 'remco' ),
    'not_found'             => __( 'Not found', 'remco' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'remco' ),
    'insert_into_item'      => __( 'Insert into item', 'remco' ),
    'uploaded_to_this_item' => __( 'Uploaded to this item', 'remco' ),
    'items_list'            => __( 'Items list', 'remco' ),
    'items_list_navigation' => __( 'Items list navigation', 'remco' ),
    'filter_items_list'     => __( 'Filter items list', 'remco' ),

);
$args = array(
    'label'                 => __( 'Events', 'remco' ),
    'description'           => __( 'Events', 'remco' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor','revisions', 'thumbnail', 'custom-fields' ),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 9,
    'menu_icon'             => 'dashicons-calendar' ,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => false,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'post',
    'taxonomies'            => 'events'

);
register_post_type( 'Events', $args );

}
add_action( 'init', 'events_post_type', 0 );

1 Ответ

0 голосов
/ 12 сентября 2018

Стандартный способ получения сообщений из пользовательского типа сообщения с пользовательскими текстами -

$args = array('post_type' => 'custom_post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'custom_taxonomy',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        ),
    ),
 );
$loop = new WP_Query($args);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...