Support » Plugins » Why get_posts not returning only selected category posts from Custom Post Type?

  • I use widget plugin Getting the random post & URL via ajax.
    Problem is that function returns all posts from CPT, so category select doesn’t work.

    php

    function get_random_post_tu() {
    
            // Simple as that, get a random post
            // I put it in an array to make it easier to read
            $args = array(
                'post_type'   => 'portfolio_item','orderby'     => 'rand',
                'numberposts' => 1
            );
    
            // Add the Category parameter, if set
            if ( isset( $_POST['usecategory'] ) && intval( $_POST['usecategory'] ) != 0 ) {
                 $args['tax_query'] = array(
              'taxonomy' => 'portfolio-category',
              'field'    => 'slug',
              'terms'    => $_POST['usecategory']
          );
            }
            $posts = get_posts( $args );
    
            /**
             * This actually gives us an array of post objects, so we should double check
             * if there is indeed a post in there.
             */
            $data = array();
            if (is_array($posts) && isset($posts[0])) {
    
                // add this to use on frontend
                $data['status']  = 'success';
                $data['link']    = get_permalink($posts[0]->ID);
                $data['title']   = get_the_title($posts[0]->ID);
                $data['thumb']   = get_the_post_thumbnail($posts[0]->ID);
                $data['content'] = get_post_field('post_content', $posts[0]->ID);
    
            } else {
    
                // add a error status
                $data['status'] = 'error';
    
            }    
    
            // use the WordPress built in function
            wp_send_json( $data ); // this is required to return a proper result
    
        }

    JQuery

    jQuery(document).ready(function($) {
        $('.grp_getnew').on('click', function(){
            var data = {
                action: 'get_random_post_tu',
                usecategory: $('#categoryselect').val()
            };
    
            $.post( ajax_object.ajax_url, data, function(response) {
                if ( response.status != 'error' ) {
                    var $link = $("<a href='" + response.link + "'>" + response.title + "</a><span >" + response.content +"</span>");
                    $('.grp_content').html($link);
                }
            }, "json");
        });
    });

    Template for output random posts

    <?php
        $args = array(
            'taxonomy' => 'portfolio-category','id' => 'categoryselect'
        );
        wp_dropdown_categories( $args );?>
    
        <button class="grp_getnew">Let's go!</button>

    Why get_posts() not returning only selected category posts from Custom Post Type? Any help,please!

  • The topic ‘Why get_posts not returning only selected category posts from Custom Post Type?’ is closed to new replies.