Follow us on

Codestar Support Forum » WordPress Plugins » Codestar Framework

Metabox post_type to include CPTs

  1. Hi there, hope all is well.

    I've noticed my Theme's Metaboxes do not appear on CPTs, and was wondering what you would suggest without having to add the CPT post_type?

    For example, I have this in my main theme:

    CSF::createMetabox( $csf_metabox_post, array(
        'title' => 'Theme Options',
        'post_type' => 'post',
        'context' => 'side',
    ));CopyCopied!

    I understand I can do this:

    'post_type' => array( 'post' , 'my_cpt' ),CopyCopied!

    but I don't want to alter my parent theme each time I have to deal with a CPT.

    Regards
    Stephan

    Posted 1 year ago #

  2. Codestar
    Admin

    Hi,

    You can add an "apply_filters" for manage it from child theme or plugin. For eg:

    CSF::createMetabox( $csf_metabox_post, array(
      'title'     => 'Theme Options',
      'post_type' => apply_filters( 'stv11c_metabox_post_types', array( 'post' ) ),
      'context'   => 'side',
    ) );CopyCopied!

    then (from child theme or plugin)

    function add_cpt_metabox_post_types( $types ) {
      $types[] = 'my_cpt';
      return $types;
    }
    add_filter( 'stv11c_metabox_post_types', 'add_cpt_metabox_post_types' );CopyCopied!

    (another way) All arguments can be control by core filters. for eg:

    function add_cpt_metabox_post_types( $args ) {
      $args['post_type'] = array( 'post', 'my_cpt' );
      return $args;
    }
    add_filter( 'csf_{YOUR_METABOX_PREFIX}_args', 'add_cpt_metabox_post_types' );CopyCopied!

    Which way you liked, use it.

    Regards, Codestar

    Posted 1 year ago #
  3. Thank you so much for the examples!
    I've decided to add an option for this in my Theme Options.

    I can retrieve the CPTs using a 'Select' with 'options' => 'post_types'

    This works great, but it also showing 'Post' and 'Page' and I only want to populate CPTs.

    I've tried adding some query_args but it's not working:

    array(
      'id' => 'opt-select-13',
      'type' => 'select',
      'title' => 'Select CPT',
      'options' => 'posts_types',
      'query_args'  => array(
        'public' => true,
        '_builtin' => false
      ),
    ),CopyCopied!

    Any ideas?

    Thanks!

    Posted 1 year ago #
  4. Ok I have this working:

    array(
      'id'          => 'opt-select-13',
      'type'        => 'select',
      'title'       => 'Select a CPT',
      'placeholder' => 'Select a CPT',
      'chosen'      => true,
      'multiple'    => true,
      'options'     => 'prefix_get_cpts_only',
    ),
    
    function prefix_get_cpts_only() {
        $args = array(
            'public'   => true,
            '_builtin' => false
        );
        $post_types = get_post_types($args, 'objects');
        $posts = array();
        foreach ($post_types as $post_type) {
            $posts[$post_type->name] = $post_type->labels->singular_name;
        }
        return $posts;
    }CopyCopied!

    If there is a simpler way with the query_args please let me know. Thanks!

    Posted 1 year ago #

  5. Codestar
    Admin

    Hi,

    Yes. No worry, it's good way. Also the framework uses similar code like yours for get post types. It's oke.

    Regards, Codestar

    Posted 1 year ago #
  6. I appreciate the response! Cheers and take care

    Posted 1 year ago #

  7. Codestar
    Admin

    You're welcome Cheers!

    Posted 1 year ago #