WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
Get all terms of custom taxonomy (7 posts)

  1. aaamirmughal
    Member
    Posted 6 months ago #

    Hi,

    I need help in retrieving all terms under the custom taxonomy. Lets say I have custom taxonomy "country" and there are about 50 countries against it, and I want to get all countries, is there a way to get those ?

    Thanks in Advance.

    http://wordpress.org/plugins/json-api/

  2. Cadu de Castro Alves
    Member
    Posted 6 months ago #

    Have you tried the get_category_index method?

  3. LaughlinJames
    Member
    Posted 5 months ago #

    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.

  4. planeturthian
    Member
    Posted 4 months ago #

    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?

  5. planeturthian
    Member
    Posted 4 months ago #

    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.

  6. tinacious
    Member
    Posted 4 months ago #

    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.

  7. planeturthian
    Member
    Posted 4 months ago #

    I agree that extending the plugin would be cleaner and more efficient. I'm on a tight deadline! :)

Reply

You must log in to post.

About this Plugin

About this Topic