Skip Navigation

[Resolved] limiting results from a query that

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 1 voice.

Last updated by julesW 5 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2783606

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

#2783618

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

This is pure custom code and I'm afraid that we do not entertain such custom code.

If you want to limit the entries to 9, what if you try to use the following:

$limit = 9;
$args = [
'post_type' => 'litter',
'numberposts' => $limit,
'post_status' => 'publish',
];

// Get the posts
$available_puppies = get_posts( $args );

More info:
- https://stackoverflow.com/questions/22408981/wordpress-get-posts-post-limit
- https://wordpress.stackexchange.com/questions/162420/how-to-limit-get-posts

If you need further help with your custom code, you are welcome to contact any of our certified partners:
- https://toolset.com/contractors/

#2783758

This is not about limiting the number of posts. The goal is to limit the number of the "litter-puppies" which is a repeatable field group. As this is a Toolset custom field, I'm hoping to get some guidance on how to proceed.

Thanks
Jules

#2783763

Hi Minesh

I figured it out. I implemented a counter that tracks how many puppies have been displayed so far. Once this counter reaches the specified limit, it breaks out of the loop.