• [ Moved to Fixing WordPress. Please do not use Everything WordPress for these break/fix topics. ]

    I am working on someone else’s WordPress Installation. They have lot of custom class/lib files added under theme (I am guessing, they might have some custom system where they can develop websites for WordPress). All of their work looks custom. I am still in kind of beginner level and not able to understand how everything works.

    Issue: I am not able to attach featured image on any blog post. (Even on custom post type)

    Already tried:

    Disabled all plugins – Still not working
    Change theme to Default – It worked
    I have deadline to develop this blog post and don’t know how to solve this.

    Thanks in advance.

    ../theme/class/libs/Post.php file

    namespace lib;
    class Posts {
    function __construct() {
    }
    
    public function create_posts($o = []) {
    
        // instantiate classes
        $metaBoxesClass = new \lib\MetaBoxes();
    
        foreach($o['posts'] as $post) {
    
            // define post type
            $postType = (isset($post['post_type'])) ? $post['post_type'] : 'page';
    
            // construct post
            $postArray = [
                'post_type' => $postType,
                'slug' => $post['slug'],
                'name' => $post['name'],
                'content' => 'This is the content for the '.$post['name'].' page.'
            ];
    
            if($o['is_sub_post'] && $o['parent_post']) {
    
                // define parent post
                $parentPost = get_page_by_path($o['parent_post'], OBJECT, $postType);
    
                // add child post array items
                $postArray['path'] = $o['parent_post'].'/'.$post['slug'];
                $postArray['parent'] = $parentPost->ID;
            }
    
            // insert top level post
            $postID = $this->create_post($postArray);
    
            // update meta boxes
            $metaBoxesClass->update_meta_boxes($post);
    
            // if post was made taxonomies were included
            if($postID && isset($post['taxonomies'])) {
    
                // for each taxonomy
                foreach($post['taxonomies'] as $taxonomy => $terms) {
    
                    // add terms to post
                    $termIDs = wp_set_object_terms($postID, $terms, $taxonomy);
                }
            }
    
            // if post has sub posts
            if(isset($post['sub_posts'])) {
    
                // run function recursively
                $this->create_posts([
                    'posts' => $post['sub_posts'],
                    'is_sub_post' => true,
                    'parent_post' => (isset($o['parent_post'])) ? $o['parent_post'].'/'.$post['slug'] : $post['slug']
                ]);
            }
        }
    
        return true;
    }
    
    public function create_post($postArray = []) {
    
        // if path, use path as full page slug, otherwise just use the slug itself
        $slug = (isset($postArray['path'])) ? $postArray['path'] : $postArray['slug'];
    
        // if post doesn't already exist
        if(!get_page_by_path($slug, OBJECT, $postArray['post_type'])) {
    
            // construct post
            $newPost = [
                'post_type' => $postArray['post_type'],
                'post_name' => $postArray['slug'],
                'post_title' => $postArray['name'],
                'post_content' => $postArray['content'],
                'post_status' => 'publish',
                'post_parent' => ($postArray['parent']) ? $postArray['parent'] : null,
            ];
    
            // create post
            $postID = wp_insert_post($newPost);
    
            return $postID;
        }
    }
    }
    

    Here is the file structure under that Class folder. I am not sure if that helps or not. Folder structure under theme

Viewing 2 replies - 1 through 2 (of 2 total)
  • It looks like the $newPost array isn’t checking for the featured image when you make a new post. Since they overwrote WordPress actions, it was probably easy to forget all that goes on with creating a new post. So the featured image never gets put in the database

    Thread Starter dhavalvyas

    (@dhavalvyas)

    Thanks for replying.

    Yes I think that’s the reason. I don’t know how I can add featured Image when wp_insert_post function runs. So that when I save with featured image it stores in database.

    Can you please point me in right direction?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom post.php file won’t save featured image on any post’ is closed to new replies.