johnbillion / extended-cpts

A library which provides extended functionality to WordPress custom post types and taxonomies.
GNU General Public License v2.0
974 stars 97 forks source link

Custom Post Type no longer appears in REST API #164

Closed nightillusions closed 3 years ago

nightillusions commented 3 years ago

As the title suggests, the custom post type "transport-zones" is no longer returning in the REST API endpoint "https://www.gabco.de/wp-json/wp/v2/transport-zones". Instead, the standard posts are returning under the this endpoint like from the endpoint "https://www.gabco.de/wp-json/wp/v2/posts". In contrast, the custom taxonomy "countries" works beautifully: https://www.gabco.de/wp-json/wp/v2/countries/

This has all worked for a year so far and recently it no longer works. I don't know if it's related to some update, anyway I didn't find any hints in the changelogs.

I would be very grateful for any help!

Code:

<?php
require __DIR__ . '/../vendor/autoload.php';

add_action( 'init', function() {

    $menu_icon = 'data:image/svg+xml;base64,' . base64_encode('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path fill="black" d="M621.3 237.3l-58.5-58.5c-12-12-28.3-18.7-45.3-18.7H464c-17.7 0-32 14.3-32 32v144H32c-17.7 0-32 14.3-32 32v27.8c0 40.8 28.7 78.1 69.1 83.5 30.7 4.1 58.3-9.5 74.9-31.7 18.4 24.7 50.4 38.7 85.3 29.7 25.2-6.5 46.1-26.2 54.4-50.8 4.9-14.8 5.4-29.2 2.8-42.4h163.2c-2.7 13.2-2.2 27.6 2.8 42.4 8.4 25.1 29.9 44.9 55.6 51.1 52.8 12.8 100-26.9 100-77.6 0-5.5-.6-10.8-1.6-16H624c8.8 0 16-7.2 16-16v-85.5c0-17.1-6.7-33.3-18.7-45.3zM80 432c-17.6 0-32-14.4-32-32s14.4-32 32-32 32 14.4 32 32-14.4 32-32 32zm128 0c-17.6 0-32-14.4-32-32s14.4-32 32-32 32 14.4 32 32-14.4 32-32 32zm320 0c-17.6 0-32-14.4-32-32s14.4-32 32-32 32 14.4 32 32-14.4 32-32 32zm-48-176v-48h37.5c4.2 0 8.3 1.7 11.3 4.7l43.3 43.3H480zM32 304h336c17.7 0 32-14.3 32-32V64c0-17.7-14.3-32-32-32H32C14.3 32 0 46.3 0 64v208c0 17.7 14.3 32 32 32zM304 80h32v176h-32V80zm-80 0h32v176h-32V80zm-80 0h32v176h-32V80zm-80 0h32v176H64V80z"/></svg>');

    register_extended_taxonomy( 'countries', 'transport-zones', array(
        'meta_box' => 'dropdown',
        'checked_ontop' => true,
        'dashboard_glance' => true,
        'admin_cols' => array(
            'title'
        ),
        'hierarchical' => false,
        'show_in_quick_edit' => false,
        'labels' => array(
            'name'                          => __( 'Länder',                                           'transport-zones' ),
            'singular_name'                 => __( 'Land',                                             'transport-zones' ),
            'menu_name'                     => __( 'Länder',                                           'transport-zones' ),
            'all_items'                     => __( 'Alle Länder',                                      'transport-zones' ),
            'edit_item'                     => __( 'Bearbeiten',                                       'transport-zones' ),
            'view_item'                     => __( 'Zeige Land',                                       'transport-zones' ),
            'update_item'                   => __( 'Land aktualisieren',                               'transport-zones' ),
            'add_new_item'                  => __( 'Neues Land erstellen',                             'transport-zones' ),
            'new_item_name'                 => __( 'Name des neuen Lands',                             'transport-zones' ),
            'search_items'                  => __( 'Suche nach Land',                                  'transport-zones' ),
            'popular_items'                 => __( 'Beliebte Länder',                                  'transport-zones' ),
            'add_or_remove_items'           => __( 'Land hinzufügen oder entfernen',                   'transport-zones' ),
            'choose_from_most_used'         => __( 'Aus den am häufigsten verwendeten Ländern wählen', 'transport-zones' ),
            'name_admin_bar'                => __( 'Länder',                                           'transport-zones' ),
            'add_new'                       => __( 'Neues erstellen',                                  'transport-zones' ),
            'new_item'                      => __( 'Neues Land',                                       'transport-zones' ),
            'not_found'                     => __( 'Kein Land gefunden',                               'transport-zones' ),
            'not_found_in_trash'            => __( 'Kein Land im Papierkorb gefunden',                 'transport-zones' ),

            'parent_item'                   => __( 'Übergeordnetes Land',                              'transport-zones' ),
            'parent_item_colon'             => __( 'Übergeordnetes Land:',                             'transport-zones' ),
            'separate_items_with_commas'    => __( 'Länder mit Kommas trennen',                        'transport-zones' ),
        ),
        'show_in_rest' => true,
    ),
    array(
        'singular' => 'Land',
        'plural'   => 'Länder',
        'slug'     => 'countries'
    ) );

    register_extended_post_type( 'transport-zones', array(

        'enter_title_here'    => 'PLZ',
        'public'              => true,
        'menu_icon'           => $menu_icon,
        'delete_with_user'    => false,
        'has_archive'         => true,
        'show_in_rest'        => true,
        'taxonomies'          => array('countries'),
        'capability_type'     => 'post',
        'map_meta_cap'        => true,
        'supports' => array(
            'title',
//          'editor',
//          'excerpt',
//          'author',
//          'thumbnail',
//          'custom-fields',
//          'revisions',
//          'page-attributes',
//          'post-formats',
        ),
        'show_in_feed' => false,
        'archive' => array(
            'nopaging' => true
        ),
        'admin_cols' => array(
            'title' => array(
                'title'          => 'Transportzone'
            ),
            'category' => array(
                'title' => 'Land',
                'taxonomy' => 'countries'
            ),
            'price_vat' => array(
                'title'    => 'Preis inkl. MwSt.',
                'function' => function() {
                    global $post;
                    $details = get_field('attributes', $post->ID);
                    echo $details['price'] . ' €';
                },
            ),
            'price' => array(
                'title'    => 'Preis zzgl. MwSt.',
                'function' => function() {
                    global $post;
                    $details = get_field('attributes', $post->ID);
                    echo ( number_format( (float) $details['price'] / 1.19 , 2, ',', '.' )  ) . ' €';
                },
            ),
            'zone' => array(
                'title'    => 'Zone',
                'function' => function() {
                    global $post;
                    $details = get_field('attributes', $post->ID);
                    echo $details['zone'];
                },
            ),
        ),

        'admin_filters' => array(
            'countries' => array(
                'taxonomy' => 'countries'
            )
        ),
        'labels' => array(
            'name'                  => __( 'Transportzonen',                                'transport-zones' ),
            'singular_name'         => __( 'Transportzone',                                 'transport-zones' ),
            'menu_name'             => __( 'Transportzonen',                                'transport-zones' ),
            'name_admin_bar'        => __( 'Transportzonen',                                'transport-zones' ),
            'add_new'               => __( 'Neue erstellen',                                'transport-zones' ),
            'add_new_item'          => __( 'Neue Transportzone erstellen',                  'transport-zones' ),
            'edit_item'             => __( 'Bearbeiten',                                    'transport-zones' ),
            'new_item'              => __( 'Neue Transportzone',                            'transport-zones' ),
            'view_item'             => __( 'Zeige Transportzone',                           'transport-zones' ),
            'search_items'          => __( 'Suche nach Transportzone',                      'transport-zones' ),
            'not_found'             => __( 'Keine Transportzone gefunden',                  'transport-zones' ),
            'not_found_in_trash'    => __( 'Keine Transportzone im Papierkorb gefunden',    'transport-zones' ),
            'all_items'             => __( 'Alle Transportzonen',                           'transport-zones' ),
            'archives'              => __( 'Transportzonenseite',                           'transport-zones' ),
            'featured_image'        => __( 'Featured Image',                                'transport-zones' ),
            'set_featured_image'    => __( 'Set featured image',                            'transport-zones' ),
            'remove_featured_image' => __( 'Remove featured image',                         'transport-zones' ),
            'use_featured_image'    => __( 'Use as featured image',                         'transport-zones' ),
            'insert_into_item'      => __( 'In Transportzone einfügen',                     'transport-zones' ),
            'uploaded_to_this_item' => __( 'Zu dieser Transportzone hochgeladen',           'transport-zones' ),
            'views'                 => __( 'Filter Transportzonenliste',                    'transport-zones' ),
            'pagination'            => __( 'Transportzonenlisten Navigation',               'transport-zones' ),
            'list'                  => __( 'Transportzonenliste',                           'transport-zones' ),

            'parent_item'        => __( 'Übergeordnete Transportzone',                      'transport-zones' ),
            'parent_item_colon'  => __( 'Übergeordnete Transportzone:',                     'transport-zones' ),
        )
    ), array(

        'singular' => 'Transportzone',
        'plural'   => 'Transportzonen',
        'slug'     => 'transport-zones'

    ) );

} );

/**
 * Hide the term description in the edit form
 */
add_action( 'countries_add_form', function( $taxonomy )
{
    ?><style>.term-description-wrap{display:none;}</style><?php
}, 10, 2 );
/**
 * Hide the term description in the post_tag edit form
 */
add_action( "countries_edit_form", function( $tag, $taxonomy )
{
    ?><style>.term-description-wrap{display:none;}</style><?php
}, 10, 2 );
/**
 * Remove the 'description' column from the table in 'edit-tags.php'
 */
add_filter('manage_edit-countries_columns', function ( $columns )
{
    if( isset( $columns['description'] ) )
        unset( $columns['description'] );

    return $columns;
} );
johnbillion commented 3 years ago

Thanks for the report. Does this problem occur if you register the post type using the native register_post_type() function from WordPress? Or does it only happen with Extended CPTs?

nightillusions commented 3 years ago

Hi, with the native register_post_type() it works. So it only happens with Extended CPTs:

function transport_zones_init() {
    $labels = array(
        'name'                  => __( 'Transportzonen',                                'transport-zones' ),
        'singular_name'         => __( 'Transportzone',                                 'transport-zones' ),
        'menu_name'             => __( 'Transportzonen',                                'transport-zones' ),
        'name_admin_bar'        => __( 'Transportzonen',                                'transport-zones' ),
        'add_new'               => __( 'Neue erstellen',                                'transport-zones' ),
        'add_new_item'          => __( 'Neue Transportzone erstellen',                  'transport-zones' ),
        'edit_item'             => __( 'Bearbeiten',                                    'transport-zones' ),
        'new_item'              => __( 'Neue Transportzone',                            'transport-zones' ),
        'view_item'             => __( 'Zeige Transportzone',                           'transport-zones' ),
        'search_items'          => __( 'Suche nach Transportzone',                      'transport-zones' ),
        'not_found'             => __( 'Keine Transportzone gefunden',                  'transport-zones' ),
        'not_found_in_trash'    => __( 'Keine Transportzone im Papierkorb gefunden',    'transport-zones' ),
        'all_items'             => __( 'Alle Transportzonen',                           'transport-zones' ),
        'archives'              => __( 'Transportzonenseite',                           'transport-zones' ),
        'featured_image'        => __( 'Featured Image',                                'transport-zones' ),
        'set_featured_image'    => __( 'Set featured image',                            'transport-zones' ),
        'remove_featured_image' => __( 'Remove featured image',                         'transport-zones' ),
        'use_featured_image'    => __( 'Use as featured image',                         'transport-zones' ),
        'insert_into_item'      => __( 'In Transportzone einfügen',                     'transport-zones' ),
        'uploaded_to_this_item' => __( 'Zu dieser Transportzone hochgeladen',           'transport-zones' ),
        'views'                 => __( 'Filter Transportzonenliste',                    'transport-zones' ),
        'pagination'            => __( 'Transportzonenlisten Navigation',               'transport-zones' ),
        'list'                  => __( 'Transportzonenliste',                           'transport-zones' ),
        // Labels for hierarchical post types only.transport-zones
        'parent_item'        => __( 'Übergeordnete Transportzone',                      'transport-zones' ),
        'parent_item_colon'  => __( 'Übergeordnete Transportzone:',                     'transport-zones' ),
    );
    $args = array(
        'labels'             => $labels,
        'description'        => 'transport-zones custom post type.',
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'transport-zones' ),
        'capability_type'    => 'post',
        'has_archive'        => false,
        'hierarchical'       => false,
        'menu_position'      => 20,
        'supports'           => array( 'title' ),
        'taxonomies'         => array( 'category', 'post_tag' ),
        'show_in_rest'       => true
    );

    register_post_type( 'transport-zones', $args );
}
add_action( 'init', 'transport_zones_init' );
nightillusions commented 3 years ago

Okay, I close this Issue. Finally I found the problem: In WP 5.7 the rest_{$this->post_type}_query hook changed in the order position, because of the new "taxquery". My `rest{$this->post_type}_query` hook for ordering the rest response by custom field was the reason for all this. After some minor changes, everything works again.

Sorry ^^

johnbillion commented 3 years ago

Thanks for letting me know 👍

kas5986 commented 3 years ago

Okay, I close this Issue. Finally I found the problem: In WP 5.7 the rest_{$this->post_type}_query hook changed in the order position, because of the new "taxquery". My `rest{$this->post_type}_query` hook for ordering the rest response by custom field was the reason for all this. After some minor changes, everything works again.

Sorry ^^

it still didn't get clear how did you solved this issue i am trying to query it through wp_query but i am not getting results for extended taxonomies using "tax_query"