Resolving in_array() Type Mismatch in Classipress Ad Listings
When customizing WordPress themes, specifically those built for listing directories like Classipress, you often encounter logic that relies on global variables to manage sticky or featured posts. The error message Warning: in_array() expects parameter 2 to be array, string given is a classic symptom of a type mismatch caused by incorrect variable handling. In the specific case of modifying loop-ad_listing.php, this warning usually stems from passing a scalar value where an indexed list is expected, or failing to properly reset state before calling the function again.
The root cause in your scenario lies in how the $featurePostArray and $postisfeatured globals are being manipulated across multiple queries. PHP's in_array() function requires the second argument to be a string representation of an integer ID if you are checking against numeric IDs, but it expects the first argument (the haystack) or the third argument (the needle type context) to align correctly. However, looking closely at the problematic line:
if (in_array($post, "ID", $featurePostArray) && $postisfeatured != "1")
echo '<div class="hide">';
There is a fundamental misunderstanding of in_array's signature here. The standard syntax is in_array(needle, haystack, strict). Your code attempts to pass "ID" as the second argument (the haystack). Since "ID" is a string literal, PHP treats it as an array with one element containing the string "ID", not as a variable holding the actual IDs of featured posts.
Furthermore, you are passing $featurePostArray as the third argument. In in_array, the third parameter ($strict) determines whether to check for type equality when checking if the needle is an integer. Passing an array as the third parameter triggers a type error because the function expects a boolean (true/false), not an array.
Correcting the Logic in loop-ad_listing.php
To fix this, you must ensure that the variable containing the list of featured post IDs is passed as the second argument, and that $post->ID (the integer ID of the current loop item) is passed as the needle. Additionally, the global variables need to be scoped correctly to avoid data leakage between iterations of the WP_Query.
Here is how the corrected logic should look inside your conditional block:
global $postisfeatured;
global $featurePostArray;
// Ensure we are pushing the ID, not the whole object if not needed,
// though usually, we store IDs in the array.
if ($postisfeatured == "1") {
array_push($featurePostArray, $post->ID);
}
// Correct usage: needle is the current post ID, haystack is the featured array.
// The third parameter should be false (non-strict) to allow integer/string comparison if needed.
if (in_array($post->ID, $featurePostArray) && !$postisfeatured) {
echo '<div class="hide">';
}
Managing Global State and Query Resetting
The second part of your code snippet involves resetting the global state after a specific query cycle:
if ($postisfeatured != "1") {
appthemes_after_endwhile();
$postisfeatured = "";
}
This logic is sound for preventing data from one WP_Query leaking into another, provided that $featurePostArray is also reset. If you are running multiple queries in the same template (e.g., one for ads and one for categories), failing to clear $featurePostArray between loops will cause in_array to return false positives based on IDs from previous queries.
You should wrap your logic in a function or ensure that $featurePostArray = [] is called immediately after the query finishes, regardless of whether the condition was met. This prevents the "string given" error from cascading if the array structure gets corrupted by unexpected data types.
Alternative Approach Using array_column
If you are working with a theme that stores featured status in post meta (common in Classipress), relying on global variables passed between loops can become fragile. A more robust approach is to query the database directly within the loop or use get_posts with specific arguments to filter by meta value without maintaining external arrays.
However, if you must stick to the global variable pattern for performance reasons (avoiding extra DB hits), ensure that $featurePostArray is strictly an array of integers. If your theme stores featured status as a string "1", cast it to int when pushing: array_push($featurePostArray, (int)$post->ID). This ensures type consistency and avoids the strict comparison issues that sometimes trip up older PHP versions or specific WordPress configurations.
For developers looking for deeper insights into customizing directory themes without breaking core logic, resources like https://laravelcompany.com offer extensive guides on WordPress development patterns that can help refine these types of conditional rendering tasks.