• I have been working with the WP REST API for the past two months and everything was going so well, I’m working on a custom post type that has some custom fields that didnt show in the rest like normally would with the WP_Post but I managed to use the add_filter() to retrieve the meta that I needed and it worked and everything was great, but now I’m trying to create my own Custom Routes and I can pull the meta that is included in the WP_Post query but not the custom fields. Is there a way to access the data on the filters? The custom post type I have it in a php file and the custom routes in another. Is that a motive for what I can’t do it?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Separate files would not be the problem, provided each file is some how included or required through the loading process.

    What you can get through filters depends on the filter. You can initially only access data that was passed to your filter callback. Of course you can access anything else from a callback assuming you have adequate initial data to know what to query for. For example, you cannot get post meta if you don’t know the related post ID.

    Since you are doing routes, your route callback could get the post meta directly with get_post_meta(). This is assuming the request contains the associated post ID or adequate information to determine the correct ID.

    Thread Starter yaffar

    (@yaffar)

    Thank you so much for your help bcworkz that worked out great, I’m almost done with my custom routes, but I need just one more thing and I’m hoping you could find me solve it. Now I need to order my query by the highest count of reviews but this is not a meta value of my post it is inside the database, I did the following to get the value and send it to the route.
    ` /* Get place rating average and amount of rates*/
    $rating_table_name = Extras\tg_get_rating_table_name();
    $avg_rating = $wpdb->get_var(
    $wpdb->prepare(
    “select COALESCE(avg(rating_rating),0) from $rating_table_name where rating_rating > 0 and rating_postid in (%d)”,$post->ID));
    $rating_count = $wpdb->get_var(
    $wpdb->prepare(
    “select COUNT(rating_id) from $rating_table_name where rating_rating > 0 and rating_postid in (%d)”,$post->ID));

    /*Get Featured image*/
    $thumbnail_id = get_post_thumbnail_id($post);
    if ($thumbnail_id) {
    $thumbnail = array();
    $thumbnail[$image_size] = wp_get_attachment_image_src($thumbnail_id, $image_size);
    }

    // Get the permalink for the blog*/
    $post->link = get_permalink($post->ID);

    /* Custom meta for custom routes*/
    $post->Mapa = get_post_meta($post->ID, ‘map’);
    $post->Usuario = $user;
    $post->Favorita = $favorite;
    $post->Bookmark = $bookmarked;
    $post->Rating = $avg_rating;
    $post->Votos = $rating_count;`

    and I’m trying something similar to make it work in the query but it isn’t. Can you help me figure it out?

    This is what I’m doing right now

    $rating_table_name = Extras\tg_get_rating_table_name();
        $rating_count = $wpdb->get_var(
                          $wpdb->prepare(
                           "select COUNT(rating_id) from $rating_table_name where rating_rating > 0 and  rating_postid in (%d)",$post->ID));
    
        $query = new WP_Query(
          array(
            'post_type' => array('post', 'place'),
            //'paged' => (isset($_GET['paged'])) ? $_GET['paged'] : 1,
            'posts_per_page' => 3,
            'orderby'   => 'meta_value_num',
            'order'      => 'DESC',
            'meta_key'  => $rating_count,
    
          )
        );

    Moderator bcworkz

    (@bcworkz)

    Because the reviews count is in a custom table, you will have difficulty getting the right order out of WP_Query. You’re better off making a custom query using global $wpdb. The query joins in the rating table so values in it can be referenced as something to order the query by. I’m not very good with SQL, so even though I know the basic concept, I myself would struggle to come up with the correct query string.

    Since you only need to get three posts at a time, if you too are bad at SQL, you could do a PHP workaround to avoid needing to compose the correct joined query. You still need to make a custom $wpdb query, but it’s much more straight forward. Initially just query the rating table for the top 3 post IDs associated with the highest ratings, ordered by rating. Single table queries are much easier to compose.

    With the top 3 post IDs, loop through the results and for each post ID get the post object with get_post(), then output whatever you want in order to list the posts — title, excerpt, date, author, etc. If you need to get more pages of posts in the same order, use OFFSET in your query to get the 4th, 5th, 6th highest ratings for page 2, for page 3, get 7, 8, 9 highest ratings, etc.

    Thread Starter yaffar

    (@yaffar)

    I’m trying to work around it using the update_post_meta() to create and fill automatically a new custom meta, but currently I’m struggling on how to make the call to update all of the posts on a single query (There are more than 30k). I’ll let you know how it goes ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get meta of custom post type for WP REST API’ is closed to new replies.