Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working for custom post types #9

Open
mehrizi opened this issue Aug 5, 2020 · 2 comments
Open

Not working for custom post types #9

mehrizi opened this issue Aug 5, 2020 · 2 comments

Comments

@mehrizi
Copy link

mehrizi commented Aug 5, 2020

Hello,
It is not working for custom post types while the Wordpress API itself works for it naturally.

@bueltge
Copy link
Owner

bueltge commented Aug 12, 2020

Do you know? Is that the filter for custom post types? - https://github.com/WordPress/WordPress/blob/1f85e7484f154912d8869886ee3e5b68b06c0bc4/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php#L254
So it should be short to enhance the filter types of the plugin.

@bueltge
Copy link
Owner

bueltge commented Aug 12, 2020

Alternatively, we get all post types and add the public post types to the filter list, like

  // For post types.
  $post_types = get_post_types( [ 'public' => true ], 'objects' );
  foreach ( $post_types as $post_type ) {

    $show_in_rest = ( isset( $post_type->show_in_rest ) && $post_type->show_in_rest ) ? true : false;
    if ( $show_in_rest ) {

      $post_type_name = $post_type->name;
      add_filter( 'rest_prepare_' . $post_type_name, [ $this, 'my_filter' ], 20, 3 );
    }
  }
  // For custom taxonomy.
  $tax_types = get_taxonomies( [ 'public' => true ], 'objects' );
  foreach ( $tax_types as $tax_type) {

    $show_in_rest = ( isset( $tax_type->show_in_rest ) && $tax_type->show_in_rest ) ? true : false;
    if ( $show_in_rest ) {

      $tax_type_name = $tax_type->name;
      add_filter( 'rest_prepare_' . $tax_type_name, [ $this, 'my_filter' ], 20, 3);
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants