I've developed code to display available puppies from a CPT called "litter." It searches through all published litter entries and only lists puppies with an "available" status. While the code functions correctly, it doesn't adhere to the specified limit.
I'm trying to limit the number of puppies displayed using an array\_slice, but it's not working.
Is there any documentation that you are following? I couldn't find anything related to limiting the number returned from a group repeater field.
Is there a similar example that we can see? Not that I'm aware of
What is the link to your site? hidden link (un and pw = labradoodles)
My code:
// Set sorting option: 'asc', 'desc', or 'random'
$sort_order = 'random'; // Change this to 'desc' or 'random' as needed
// Set the limit for the number of puppies to show
$limit = 9; // Change this to the desired number of puppies
$args = [
'post_type' => 'litter',
'numberposts' => -1,
'post_status' => 'publish',
];
// Get the posts
$available_puppies = get_posts( $args );
// Initialize output variable
$available_puppies_output = '';
// Sort the puppies based on the specified order
if ($sort_order === 'asc') {
usort($available_puppies, function($a, $b) {
return strcmp($a->post_title, $b->post_title);
});
} elseif ($sort_order === 'desc') {
usort($available_puppies, function($a, $b) {
return strcmp($b->post_title, $a->post_title);
});
} elseif ($sort_order === 'random') {
shuffle($available_puppies);
}
// Limit the number of puppies to display
$available_puppies = array_slice($available_puppies, 0, $limit);
foreach ( $available_puppies as $puppy ) {
// Retrieve related puppies using Toolset
$puppy_puppy_data = toolset_get_related_posts( $puppy->ID, 'litter-puppies', array( 'query_by_role' => 'parent', 'return' => 'post_object' ) );
// Check if puppy data is not empty
if ( ! empty( $puppy_puppy_data ) && is_array( $puppy_puppy_data ) ) {
foreach ( $puppy_puppy_data as $puppy_data ) {
// Ensure puppy_data is an object and has the expected properties
if ( is_object( $puppy_data ) ) {
// Retrieve puppy status using Toolset's types_render_field
$puppy_status = get_post_meta( $puppy_data->ID, 'wpcf-litter-puppy-status', true );
// Check if the puppy is available
if ( "available" === trim( $puppy_status ) ) {
$puppy_litter = get_post_meta( $puppy->ID, 'wpcf-litter-name', true );
$puppy_nick_name = types_render_field( "litter-puppy-nickname", array( 'item' => $puppy_data->ID ) );
// Use shortcode to get the puppy image
$shortcode = str_replace('%1$s', $puppy_data->ID, '[types field="litter-puppy-image" item="%1$s" size="medium" alt="%%ALT%%" output="html"]');
$puppy_image_html = do_shortcode($shortcode);
// Create a link to the litter entry page
$puppy_litter_link = get_permalink( $puppy->ID );
$available_puppies_output .= sprintf(
'<div class="et_pb_module et_pb_blurb availble puppy-item %4$s">
<div class="et_pb_blurb_content">
<div class="ribbon ribbon-top-left">
<span>%4$s</span>
</div>
<h3 class="et_pb_module_header nickname">%2$s</h3>
<div class="et_pb_main_blurb_image">
<span class="et_pb_image_wrap">%3$s</span>
</div>
<div class="et_pb_blurb_container">
<div class="et_pb_blurb_description">
<div class="full-name">%1$s</div>
<div class="et_pb_button_module_wrapper et_pb_button_alignment_center et_pb_module ">
Visit Litter Page
</div>
</div>
</div>
</div>
</div>',
esc_html( $puppy_litter ),
esc_html( $puppy_nick_name ),
$puppy_image_html, // Output the image from the shortcode
esc_attr( $puppy_status ),
esc_url( $puppy_litter_link ) // Link to the litter entry page
);
}
}
}
}
}
if ( $available_puppies_output !== '' ) {
echo sprintf( '<div class="puppy-container" data-lazy="false">%1$s</div>', $available_puppies_output );
} else {
echo sprintf( '<div class="et_pb_module et_pb_text et_pb_text_align_left et_pb_bg_layout_light"><div class="et_pb_text_inner"><p class="no-results">There are no available puppies at this time.</p></div></div>' );
}
Is this something you can help me with?
Thank you!
Jules