I honestly don’t know what problem you are trying to solve. It is not clear what you are trying to accomplish with this code, but:
echo count($the_query_partnerzy);
really is counting elements in this query, but shows only 1 (weird).
And having that, I’m not able to move further than this.
No, its not. WP_Query
returns an object and count
doesn’t work on Objects, mostly.
The actual query results are in $the_query_partnerzy->posts
so count($the_query_partnerzy)
would give you what you want, but the WP_Query
object already provides that data for you. Use $the_query_partnerzy->found_posts
instead.
I cleaned up your code a bit, and this does work using the ‘post’ post type (so I could run the code and test it):
$catArgs = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$categoryList = get_categories( $catArgs );
echo 'Sum: '.count($categoryList);
$i = 0;// debugging ?
for ($categoryList as $catl) {
echo '<br>Number of loop: ' . $i; // debugging ?
echo '<br>Category name: ' . $catl->name;
$the_query_partnerzy = new WP_Query(
array(
'post_type' =>'post',
'category_name' => $catl->name
)
);
echo $the_query_partnerzy->found_posts; // debugging ?
if ($the_query_partnerzy->have_posts()) {
echo 'I got it!'; // debugging ?
echo '<div class="post-list container">';
echo '<p class="post-title">'. $catl->name .'</p>';
while ($the_query_partnerzy->have_posts()) {
$the_query_partnerzy->the_post();
echo the_post_thumbnail();
} // while
echo '</div>';
} // query partnerzy
$i++; // debugging ?
}
A few notes:
- The lines marked
// debugging ?
most certainly should be removed. - The type of
for
loop you are using, while valid, is rarely
necessary in PHP. There is a much more convenientforeach
, and
thatfor
loop was causing some errors as (for reasons I have not
investigated) the array given back byget_categories()
started
with1
and not0
. Withforeach
you don’t have to worry about
that. -
You were missing quotes in your query arguments. That is, this:
$the_query_partnerzy = new WP_Query( array( post_type => 'post', category_name => $catl->name ) );
Instead of this:
$the_query_partnerzy = new WP_Query( array( 'post_type' => 'post', 'category_name' => $catl->name ) );
While that should have worked it was spitting out warning like
crazy. - You are pulling categories from the
post
post type, but later are
trying to pull posts from a different post type having those
categories. Are you sure that the categories are registered for both
post types and that there are in fact posts that fit that logic?
That said, the code is good and works for me.