Follow us on

Codestar Support Forum » WordPress Plugins » Codestar Framework

Select and Custom Post Types

  1. Wordpress states that Custom Post Types should not be created any earlier than the "init" action. So, I register mine in there without an issue.

    What I am noticing though, is that my custom Admin Options appear to be firing much earlier than that, so when I attempt to build out a select field, and use "post_types" as my "options", they are only populated with "Page" and "Post", because at that point in time, the CPTs are not yet registered.

    Generally, what I do when I need to fire up said options with the fields, I do it in the "csf_loaded" action, as all the rest end up throwing me a "You cannot view this page" message (including the later firing csf_enqueue). This way, I know for a fact that CSF is indeed loaded up, running, and available to me.

    So, my question is... where (what action) should I be creating my CPTs in this case? Even if I use "csf_loaded" to register the CPT's, with a lower priority than I use for the Admin Options, the field does not populate with them, along with a higher priority.

    I've attached screenshots showing the CPTs, and field.

    Posted 1 year ago #
  2. Admin Options appear to be able to be fired up and built in this set of actions only the latest being "set_current_user".

    Meaning, it can never get Custom Post types, when said CPT's are properly registered in the "init" action and after. However, I can see in the reference action array what fires off and where, and it looks like Automattic doesn't appear to follow their own guidelines when I comes to registering post types.

    Example here: https://wordpress.stackexchange.com/questions/162862/how-to-get-wordpress-hooks-actions-run-sequence

    So... I've got some more digging to do, I just wanted to let you know this all before I forget about it.

    Posted 1 year ago #
  3. I don't think there is anything that can get done about this. I also no longer believe it's on CSF's end.

    Settings/Options should be created prior to the "init" action anyways, along with before a user is even authenticated (which seems a bit strange to me...). Along with Post and Page getting created looooong before anything else happening

    So... in case anyone needs it, here is an alternative that I have come up with. This is only valid for Custom Post Types.

    /**
     * get_post_types_for_select
     *
     * Public method pull to gather all public post types for a select box
     *
     * @since 7.3
     * @access public
     * @author Kevin Pirnie <me@kpirnie.com>
     *
     * @return array Returns an array of posts types
     *
    */
    public function get_post_types_for_select( ) : array {
    
    	// setup a return array
    	$_ret = array( );
    
    	// the first item needs to be NONE
    	$_ret['none'] = __( ' -- None -- ' );
    
    	// see if we've already got this in cache
    	$_post_types = wp_cache_get( 'kpcp_post_types', 'kpcp_post_types' );
    
    	// check if we're already cached
    	if( $_post_types ) {
    
    		// we are, so return the object
    		return $_post_types;
    
    	// we aren't cached yet
    	} else {
    
    		// get the inherent post types
    		$_the_pts = get_post_types( array( '_builtin' => false, 'public' => true, ), 'names' );
    
    		// check if we have any
    		if( empty( $_the_pts ) ) {
    
    			// we don't yet because they aren't registered as of this point, we'll need to try to get them from the existing posts
    
    			// fire up the wpdb global
    			global $wpdb;
    
    			// run a query to get all post types
    			$_pts = $wpdb -> get_results( "SELECT DISTINCT post_type FROM $wpdb->posts WHERE post_status = 'publish' AND post_type NOT IN ( 'nav_menu_item', 'page', 'post', 'acf-field-group', 'attachment', 'custom_css', 'customize_changeset' ) ORDER BY post_type ASC;", ARRAY_A );
    
    			// make sure we have some
    			if( $_pts ) {
    
    				// loop over them
    				foreach( $_pts as $_pt ) {
    
    					// add the post type to the returnable array
    					$_ret[ $_pt['post_type'] ] = ucwords( __( str_replace( '_', ' ', $_pt['post_type'] ) ) );
    
    				}
    
    			}
    
    		} else {
    
    			// loop over them
    			foreach( $_the_pts as $_pt ) {
    
    					// add the post type to the returnable array
    					$_ret[ $_pt ] = ucwords( __( str_replace( '_', ' ', $_pt ) ) );
    
    			}
    
    		}
    
    		// set the array to the cache for 1 hour
    		wp_cache_add( 'kpcp_post_types', $_ret, 'kpcp_post_types', HOUR_IN_SECONDS );
    
    	}
    
    	// return the array
    	return ( is_array( $_ret ) ) ? $_ret : array( );
    
    }CopyCopied!
    Posted 1 year ago #

  4. Codestar
    Admin

    Hi,

    Yes, I got the issue. Hmm actually, I have followed other popular plugins for start to "init". It's best action to fire it. All uses same.

    But still it's not clear. for eg. I'm creating simply CPT like this:

    function register_portfolio_cpt() {
    
      $args = array(
        'labels' => array(
          'name' => 'Portfolio',
        ),
        'public' => true,
      );
    
      register_post_type( 'portfolio', $args );
    
      $args = array(
        'labels' => array(
          'name' => 'Products',
        ),
        'public' => true,
      );
    
      register_post_type( 'products', $args );
    
    }
    
    add_action( 'init', 'register_portfolio_cpt' );CopyCopied!
    array(
      'id'          => 'opt-select-cpt',
      'type'        => 'select',
      'title'       => 'Select with post type',
      'placeholder' => 'Select a post type',
      'options'     => 'post_types',
    ),CopyCopied!

    and I can see it work normally. Can I see your setup detailed ? Send a lite version of project or an example. why you need "csf_loaded" I am wondering.

    Regards, Codestar

    Posted 1 year ago #
  5. Are you on gitlab?

    Posted 1 year ago #

  6. Codestar
    Admin

    Hi,

    Uh no, you can send via routewp@gmail.com

    Regards, Codestar

    Posted 1 year ago #
  7. I'm all set with this mate. What I'm going to end up doing is overriding the setup class, and rework some of the firing of the sections in there

    Thanks!

    Posted 1 year ago #

  8. Codestar
    Admin

    You're welcome

    Posted 1 year ago #