editing shortcode for custom post type within a plugin

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 show=”” to 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!

Read more here:: editing shortcode for custom post type within a plugin

Leave a Reply

Your email address will not be published. Required fields are marked *