0

Here's a question I've been trying to solve all day:

I added a WP Query that would show posts from a category that the admin chooses with a custom field. The post is a custom post type called "Testimonials", and the category is a custom category called "Testimonial Categories". The admin can select multiple categories, and all the relevant testimonials should appear on the page.

The code mostly works, but 1 testimonial is missing. Even stranger, the missing testimonial is different between staging and production sites (the content is exactly the same).

It seems that when a field gets updated on the missing testimonial, that testimonial makes it to the page. But a different testimonial goes missing. Also very strange behavior.

Here's the WP Query:

@php 
    $cats = get_sub_field('reviews_category'); // gets the custom field categories
    $args = array(
                'post_type' => 'testimonials',
                'numberposts' => -1,
                'order' => 'DESC',
                'orderby' => 'meta_value',
                'meta_key' => 'date',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'testimonial_categories',
                        'field' => 'term_id',
                        'terms' => $cats
                    ),
                )
            );
    $query = new WP_Query($args);
@endphp

Thanks in advance for your help. Let me know if I am missing information that can be useful.

1
  • 1
    numberposts is not a valid parameter. You need to use posts_per_page. Commented Nov 29, 2023 at 7:06

1 Answer 1

1

@jacob-peattie answered the question in his comment - I used the wrong key to set the number of posts my query generates. Swapping numberposts with posts_per_page solved the issue.

Here's the final query:

@php 
    $cats = get_sub_field('reviews_category'); // gets the custom field categories
    $args = array(
                'post_type' => 'testimonials',
                'posts_per_page' => -1, // this line updated
                'order' => 'DESC',
                'orderby' => 'meta_value',
                'meta_key' => 'date',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'testimonial_categories',
                        'field' => 'term_id',
                        'terms' => $cats
                    ),
                )
            );
    $query = new WP_Query($args);
@endphp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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