• niel

    (@nielfernandez)


    Hi, I am planing to fetch base on custom category or taxonomy correct me if I am wrong named “genre”, and this is how I do it

    $arg =   get_posts( array(
            
          'post_type'  => 'comment',
          'tax_query'  => array(
            array(
                'taxonomy' => 'genre',
                'field'    =>  'id',
                'terms'    =>  13,
            )),
          'post_status'    => 'publish',
          'orderby'        => 'date',
          'order'          => 'DESC',
        
         ));
    
         $url    = add_query_arg( $arg , get_home_url()."/wp-json/wp/v2/comment?".$filter);
    
         // A standard GET request the WordPress way
         $stuff = wp_remote_get($url);
    
         // Get just the body of that request (if successful)
         $body = wp_remote_retrieve_body($stuff);
    
         // Turn the returned JSON object into a PHP object
         $posts = json_decode($body);

    As you can see it is api from custom post type the main goal is to display all post through shortcode display all data from custom post type base on custom category named “genre” but it wont work please help how can I fetch data from custom post type base on category custom category.

    Ex. From post type post have category name car display all only have category car

    What I need is from custom post type name art display only have category name “digital” but that category was made in custom register_taxonomy

    Please thank for help.

    • This topic was modified 2 years, 11 months ago by t-p. Reason: Moved to Fixing Wordpress from Developing with WordPress
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The API route for comments wouldn’t do what you want if you really want posts. Also, adding query args from a nested array doesn’t work right because the nested relationship is lost. One more issue, adding query args from the results of get_posts() is illogical, you essentially have a mixed data type error.

    While you could in theory get what you want from an API request, if this is all for the same site it doesn’t make a lot of sense. Just directly query for what you want and bypass the API.

    Do you really have a post type named “comment”? WP comments are not posts, they are related to posts but reside in their own table. WP comments are not normally associated with taxonomies, only posts. The related posts of course could be related to taxonomies. If you need comments related to posts assigned a particular taxonomy term, you’d first query for the related posts to get an array of post IDs (using the ‘fields’ arg), then query for the comments using that array (using ‘post__in’ arg).

Viewing 1 replies (of 1 total)
  • The topic ‘add_query_arg OR tax_query NOT Working’ is closed to new replies.