Good Day,
I am really hoping someone can help me as I have spent a heap of time trying to fix this.
I have created Store Locator Functionality using ordinary posts that utilise custom fields using the Advanced Custom Fields Plugin.
I have compiled functions that count stores (posts) depending on the selection of a radio button group that is added to normal post edit pages via the Advanced Custom Fields Plugin. These functions are in my functions.php document and they have been created as shortcodes to be used in a specific page.
This Radio Button Group will allow you to select one of two options - Member or Wholly Owned. It is set to select Wholly Owned by default. So upon creating the post, Wholly Owned is already selected and you'll only change it if you'd prefer it to be Member.
I know what the end amount should be, the purpose of the code is to increment the relevant value every time you add or remove a new store.
I'm pretty sure I had this code returning the correct value at one stage, but now it seems to be adding an extra value. It is meant to return 198 but it is returning 199.
The Member count is spot on, it returns the expected 32.
Here is what I can say for sure:
• The code for each function is identical.
• I have 'Returned' all of the 'Titles' of 'Wholly Owned' and it has returned 198 results.
• When I filter the posts in my back end to display only posts under the 'Store Locator' category, it shows 230 posts, when you subtract 32, which is the 'Member' amount, you get 198.
• I have set 'post_status' to 'publish' so it shouldn't be fetching deleted Posts, and I do not have any Store Locator posts in my trash folder.
• Only Store Locator Posts can contain these Meta Values as the Advanced Custom Fields will only Appear when a Sub-Category of Store Locator is selected.
Please let me know if you are able to shed some light on this, I am baffled. Please also let me know if you need any further information?
Thanking you in advance :)
Please see my code below:
// Function to count Wholly Owned Stores
function count_wholly_owned_stores() {
$wholly_owned_stores = get_posts(array(
'post_type' => 'post',
'meta_key' => 'wholly_owned/member',
'meta_value' => 'Wholly Owned',
'posts_per_page' => -1,
'post_status' => 'publish',
'post_parent' => 'store-locator'
));
$number_of_wholly_owned = count($wholly_owned_stores);
return $number_of_wholly_owned;
}
wp_reset_query();
add_shortcode('amt_wholly_owned', 'count_wholly_owned_stores');
// Function to count Member Owned Stores
function count_member_owned_stores() {
$member_owned_stores = get_posts(array(
'post_type' => 'post',
'meta_key' => 'wholly_owned/member',
'meta_value' => 'Member',
'posts_per_page' => -1,
'post_status' => 'publish',
'post_parent' => 'store-locator'
));
$number_of_member_owned = count($member_owned_stores);
return $number_of_member_owned;
wp_reset_query();
}
add_shortcode('amt_member_owned', 'count_member_owned_stores');