admin管理员组

文章数量:1426338

I made a custom post type filters for others post types. The slug is a concatenation like "filters-" . $custom-post-type.

And the custom post type appears correctly in the menu :

My problem is when I try to save one, it is saved with post_type = post. What I missed up ?

This is my PHP Code :

class SearchFilters
{


    public $post_type_slug;
    public $post_type_label_name;
    public $post_type_singular_name;

    public $current_plugin_domain;

    public function __construct( $post_type_slug, $post_type_label_name, $post_type_singular_name )
    {
        $this->current_plugin_domain = get_current_plugin_domain();

        $this->post_type_slug = $post_type_slug;
        $this->post_type_label_name = $post_type_label_name;
        $this->post_type_singular_name = $post_type_singular_name;
        $this->init();
    }

    private function init(){
        $filters_slug = "filters-" . $this->post_type_slug;
        $filters_args = [
            'labels' => array(
                'name'          => sprintf( __( "Filters - Search Form for %s", $this->current_plugin_domain  ), $this->post_type_label_name   ),
                'singular_name' => __( "Filter", $this->current_plugin_domain  ),
                'menu_name'          => __( "Filters", $this->current_plugin_domain  ),
                'add_new_item'          => sprintf( __( "Filters - Add New Search Form for %s", $this->current_plugin_domain  ), $this->post_type_singular_name  )
            ),
            'description' => __( "Filters to display search form on front end", $this->current_plugin_domain ),
            'supports' => array( 'title' ),
            'public' => false,
            'show_ui' => true,
            'show_in_menu' => "edit.php?post_type=" . $this->post_type_slug,
            'auto_save' => false
        ];
        $result_filter = register_post_type(
            $filters_slug,
            $filters_args
        );
        if ( is_wp_error( $result_filter ) ) {
            wp_error_log( $result_filter->get_error_message(), "Filter Post type creation " . "[" . __CLASS__ . "]" );
        }

    }
}

I made a custom post type filters for others post types. The slug is a concatenation like "filters-" . $custom-post-type.

And the custom post type appears correctly in the menu :

My problem is when I try to save one, it is saved with post_type = post. What I missed up ?

This is my PHP Code :

class SearchFilters
{


    public $post_type_slug;
    public $post_type_label_name;
    public $post_type_singular_name;

    public $current_plugin_domain;

    public function __construct( $post_type_slug, $post_type_label_name, $post_type_singular_name )
    {
        $this->current_plugin_domain = get_current_plugin_domain();

        $this->post_type_slug = $post_type_slug;
        $this->post_type_label_name = $post_type_label_name;
        $this->post_type_singular_name = $post_type_singular_name;
        $this->init();
    }

    private function init(){
        $filters_slug = "filters-" . $this->post_type_slug;
        $filters_args = [
            'labels' => array(
                'name'          => sprintf( __( "Filters - Search Form for %s", $this->current_plugin_domain  ), $this->post_type_label_name   ),
                'singular_name' => __( "Filter", $this->current_plugin_domain  ),
                'menu_name'          => __( "Filters", $this->current_plugin_domain  ),
                'add_new_item'          => sprintf( __( "Filters - Add New Search Form for %s", $this->current_plugin_domain  ), $this->post_type_singular_name  )
            ),
            'description' => __( "Filters to display search form on front end", $this->current_plugin_domain ),
            'supports' => array( 'title' ),
            'public' => false,
            'show_ui' => true,
            'show_in_menu' => "edit.php?post_type=" . $this->post_type_slug,
            'auto_save' => false
        ];
        $result_filter = register_post_type(
            $filters_slug,
            $filters_args
        );
        if ( is_wp_error( $result_filter ) ) {
            wp_error_log( $result_filter->get_error_message(), "Filter Post type creation " . "[" . __CLASS__ . "]" );
        }

    }
}
Share Improve this question asked Jun 19, 2019 at 12:08 J.BizMaiJ.BizMai 9002 gold badges10 silver badges30 bronze badges 2
  • How are you calling the class to register "Properties" ? – Kumar Commented Jun 19, 2019 at 12:25
  • "Properties" works perfectly. I´m using register_post_type to declare as well. – J.BizMai Commented Jun 19, 2019 at 12:32
Add a comment  | 

2 Answers 2

Reset to default 0

The problem seems to be link to a jQuery script I did to check things on post saving.

I forgot :

$postForm.unbind('submit');

Please try below code :

class SearchFilters
{


    public $post_type_slug;
    public $post_type_label_name;
    public $post_type_singular_name;

    public $current_plugin_domain;

    public function __construct( $post_type_slug, $post_type_label_name, $post_type_singular_name )
    {
        $this->current_plugin_domain = get_current_plugin_domain();

        $this->post_type_slug = $post_type_slug;
        $this->post_type_label_name = $post_type_label_name;
        $this->post_type_singular_name = $post_type_singular_name;
        $this->init();
    }

    private function init(){
        $filters_slug = "filters-" . $this->post_type_slug;
        $filters_args = [
            'labels' => array(
                'name'          => sprintf( __( "Filters - Search Form for %s", $this->current_plugin_domain  ), $this->post_type_label_name   ),
                'singular_name' => __( "Filter", $this->current_plugin_domain  ),
                'menu_name'          => __( "Filters", $this->current_plugin_domain  ),
                'add_new_item'          => sprintf( __( "Filters - Add New Search Form for %s", $this->current_plugin_domain  ), $this->post_type_singular_name  )
            ),
            'description' => __( "Filters to display search form on front end", $this->current_plugin_domain ),
            'supports' => array( 'title' ),
            'public' => false,
            'show_ui' => true,
            'exclude_from_search' => true,
            'show_in_admin_bar'   => false,
            'show_in_nav_menus'   => false,
            'publicly_queryable'  => false,
            'query_var'           => false,
            'show_ui' => true,
            'menu_icon' => 'dashicons-tag',
            'capability_type' => 'post',
            'rewrite' => array( 'slug' => $this->post_type_slug),
            'show_in_menu' => "edit.php?post_type=" . $this->post_type_slug,
            'auto_save' => false
        ];
        $result_filter = register_post_type(
            $filters_slug,
            $filters_args
        );
        if ( is_wp_error( $result_filter ) ) {
            wp_error_log( $result_filter->get_error_message(), "Filter Post type creation " . "[" . __CLASS__ . "]" );
        }

    }
}

本文标签: Custom post types saved as basic post