Viewing 6 replies - 1 through 6 (of 6 total)
  • Have you tried the get_category_index method?

    Cadu,

    I can’t speak for the original poster, but get_category_index won’t work for me. I need to get a list of taxonomy terms for a given post type. For example, in my WordPress admin console, I get a list of the categories by going to:

    wp-admin/edit-tags.php?taxonomy=portfolio-category&post_type=portfolio-item

    This gives me all of the “portfolio-categories” that I can assign to a “portfolio-item”.

    How can I get this from the api?

    Thanks.

    James.

    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?

    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.

    If you are building a JavaScript app, and if what you’re looking for is available in the larger JSON object, you can iterate through the whole response and check for matching key-value pairs. This would be done primarily client-side though, rather than with WordPress.

    Alternatively, you can create a separate WordPress plugin that extends the current JSON API methods and create additional controllers that return an array of the result of your desired queries.

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get all terms of custom taxonomy’ is closed to new replies.