Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • planeturthian

    (@planeturthian)

    I agree that extending the plugin would be cleaner and more efficient. I’m on a tight deadline! ??

    planeturthian

    (@planeturthian)

    Ok, so I found a solution. The idea, is that for each type of post, there’s a separate json file that contains all of the data for that post type. The file is generated when someone saves a post.

    You can trigger the save like so: add_action('save_post','saveJSON');

    function getAllPostsOfType($postType){
        $results = array();
        $args=array(
            'post_type' => $postType,
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        $posts = $my_query->posts;
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        foreach($posts as $post){
            $url = get_permalink($post->ID) . "&json=1&count=-1";
            curl_setopt($ch, CURLOPT_URL,$url);
            $result=json_decode(curl_exec($ch));
            array_push($results, $result->post);
        }
        wp_reset_query();
        return $results;
    }
    
    function saveJSON( $postId ){
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $postId;
        // Check the user's permissions.
        if ( 'page' == $_POST['post_type'] ) {
            if ( ! current_user_can( 'edit_page', $postId ) )
                return $postId;
        } else {
            if ( ! current_user_can( 'edit_post', $postId ) )
                return $postId;
        }
        /* OK, its safe for us to save the data now. */
        $postType = $_POST["post_type"];
        $data = getAllPostsOfType($postType);
    
        file_put_contents(get_template_directory()."/JSON/".$postType.'.json', json_encode($data, JSON_PRETTY_PRINT));
    }

    Notice i’m still using the JSON API, by accessing the data through curl.

    An added bonus to this technique is that it’ll lighten the load on your database. You only call the database when there’s a new post. After that all the data is stored in the json file.

    planeturthian

    (@planeturthian)

    I’m also looking for a similar solution. I found a dirty way to do it, but I don’t like it.

    What I did is I looped through all posts of a particular type and I pushed the url into a javascript array that I can access later.

    var products = Array();
    
        <?php
                    $type = 'Products';
                    $args=array(
                        'post_type' => $type,
                        'post_status' => 'publish',
                        'posts_per_page' => -1,
                        'caller_get_posts'=> 1
                    );
    
                    $my_query = null;
                    $my_query = new WP_Query($args);
                    if( $my_query->have_posts() ) {
                        while ($my_query->have_posts()) : $my_query->the_post(); ?>
                            products.push("<?php the_permalink()."&json=1"; ?>");
                        <?php
                            endwhile;
                        }
                            wp_reset_query();  // Restore global post data stomped by the_post().
            ?>

    With that, you can loop through the array and get all the json data. Another solution I though of would be to create an event when there’s a new post, and a json file would be generated and saved to disk as a .json file. You could easily retrieve the file and arrange it however you like.

    I don’t really like either of these solutions. Has anybody found a more elegant one?

Viewing 3 replies - 1 through 3 (of 3 total)