0

My head is spinning on this code. I'm trying to add the ability to an existing plugin's shortcode, to display only certain custom post types that contain a certain category name.

So far, I have this:

   public function shortcode_litters( $args ) {

    $output = '';
    $atts = shortcode_atts( array(
            'category' => ''
    ), $atts );
    $terms = get_terms('product_category');
            wp_reset_query();

    $query_args = array( 
        'post_type'         => 'litter',
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'order'             => 'asc',
        'numberposts'       => -1,
        'tax_query' => array(
                 array(
                    'taxonomy' => 'product_category',
                    'field' => 'slug',
                    'terms' => $atts,
                ),
             ),

    );

    $litters = get_posts($query_args);

    if($litters) {

        $columns = empty($args["columns"]) ? 2 : $args["columns"];

        $output .= '<div class="breedr"><ul class="columns '.number_to_words($columns).'">';

        foreach ( $litters as $litter ) {
            $output .= $this->breedr_template( plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/list-single-litter.php', $litter );
        }

        $output .= '</ul></div>';

    }
    else {
        $output .= "<span>No litter found.</span>";
    }

    return $output;
}

originally it was this:

  public function shortcode_litters( $args ) {

    $output = '';
    $query_from_date = strtotime(date("Y-m-d", strtotime("-3 months")));

    $query_args = array( 
        'post_type'         => 'litter',
        'post_status'       => 'publish',
        'orderby'           => 'title',
        'order'             => 'asc',
        'numberposts'       => -1
    );

    if(!empty($args['show']) && in_array($args['show'], array('current', 'past'))) {
        if ($args['show'] == "current") { 
            $meta_query = array(
                'meta_query' => array(
                    array(
                        'key'       => '_litter_mating',
                        'value'     => $query_from_date,
                        'compare'   => '>=',
                        'type'      => 'NUMERIC'
                    )
                )
            );
        }
        if ($args['show'] == "past") {
            $meta_query = array(
                'meta_query' => array(
                    array(
                        'key'       => '_litter_mating',
                        'value'     => $query_from_date,
                        'compare'   => '<',
                        'type'      => 'NUMERIC'
                    )
                )
            );
        }
    }
    else {
        $meta_query = array('meta_query' => '');
    }

    $litters = get_posts($query_args + $meta_query);

    if($litters) {

        $columns = empty($args["columns"]) ? 2 : $args["columns"];

        $output .= '<div class="breedr"><ul class="columns '.number_to_words($columns).'">';

        foreach ( $litters as $litter ) {
            $output .= $this->breedr_template( plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/list-single-litter.php', $litter );
        }

        $output .= '</ul></div>';

    }
    else {
        $output .= "<span>No litter found.</span>";
    }

    return $output;
}

Essentially, I'm changing the shortcode from [litters show=""] to [litters category=""] - as I want it to display via category name (not number) in the shortcode. Originally, it has show="" (current or past) and it determines which one it is based on the date. I want to manually be able to determine if it is current or past by selecting a category name via the shortcode.

I've been trying to figure this out all day, and I'm brain dead at this point. Any help would be appreciated!

0

In your code $atts is always $atts = [ 'category' => '' ], you cannot use 'terms' => $atts, in query parameters $query_args.

[litters category="first_slug,second_slug"]

You can assign to 'terms' in 'tax_query' parameter a string (single category) or an array (multiple category). $args['category'] is passed to the shortcode function as string, so you need to check if it's one category and no action is required, or a list of slugs separated by commas and they must be converted to an array.

public function shortcode_litters( $args ) {

    $output = '';
    //
    // filter shortcode attributes
    $args = shortcode_atts( 
        array( 'category' => '', 'columns' => '' ), 
        $args 
    );
    if ( strpos( $args['category'], ',' ) !== false ) {
        $terms = explode( ',', $args['category'] );
    } else {
        $terms = $args['category'];
    }

    $query_args = array( 
        'post_type'         => 'litter',
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'order'             => 'asc',
        'numberposts'       => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_category',
                'field'    => 'slug',
                'terms'    => $terms,
            ),
        ),
    );
  • This worked like magic, THANK YOU! – Air Aug 28 at 16:37
  • Actually, all works until I add two categories : [litters category="first_slug,second_slug"] This breaks it, and I get these errors: Warning: explode() expects at least 2 parameters, 1 given in /data/27/5/118/151/5444477/user/6515207/htdocs/wp-content/plugins/breedr/public/class-breedr-public.php on line 475 Warning: explode() expects at least 2 parameters, 1 given in /data/27/5/118/151/5444477/user/6515207/htdocs/wp-content/plugins/breedr/public/class-breedr-public.php on line 475 No litter found. – Air Aug 28 at 17:41
  • Perfect! Thanks again, you're amazing :) – Air Aug 28 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.