Title: Count Function returning incorrect value from Get Posts Function
Last modified: August 31, 2016

---

# Count Function returning incorrect value from Get Posts Function

 *  Resolved [MelmoSA](https://wordpress.org/support/users/melmosa/)
 * (@melmosa)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/count-function-returning-incorrect-value-from-get-posts-function/)
 * 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');
       ```
   

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/count-function-returning-incorrect-value-from-get-posts-function/#post-7295808)
 * The only explanation I can think of is there is a store that has both values 
   for ‘wholly_owned/member’ field. We normally think of each field as having a 
   single value, but it’s possible to have multiple values under one key.
 * Try making a query that includes the ‘meta_query’ argument where the field is
   both Wholly Owned AND Member. If I’m right, it’ll return the problem store. To
   fix it, you may need to go directly to the DB with phpMyAdmin. Search for the
   post ID in the post_meta table and both values should come up. Delete the Wholly
   Owned value so only Member is assigned to that post.
 *  Thread Starter [MelmoSA](https://wordpress.org/support/users/melmosa/)
 * (@melmosa)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/count-function-returning-incorrect-value-from-get-posts-function/#post-7295812)
 * Hi bcworkz,
 * Thank you for the response.
 * Would it be possible for a store to have both values even though the values are
   generated by radio buttons of which you can only select one value?
 * Either way, I will try what you suggested.
 * Thank you, I really do appreciate it.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/count-function-returning-incorrect-value-from-get-posts-function/#post-7295836)
 * Yes, if `add_post_meta()` is called more than once without the fourth parameter
   set to `true`. For example, a store is added with Member selected, and the field
   value is saved with `add_post_meta($post->ID, 'wholly_owned/member', 'Member');`
 * A mistake is realized and the store is edited with the field changed to Wholly
   Owned. On update, the value is saved with `add_post_meta($post->ID, 'wholly_owned/
   member', 'Wholly Owned');` . This will result in two rows in post meta with the
   same post ID and key, but different values.
 * Values should be saved with `add_post_meta($post->ID, 'wholly_owned/member', 
   $value, true);`, or better yet, use `update_post_meta()`. If update is used where
   no key/value yet exists, it is added. The resulting code is less complicated 
   and less prone to undesired multiple values.
 *  Thread Starter [MelmoSA](https://wordpress.org/support/users/melmosa/)
 * (@melmosa)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/count-function-returning-incorrect-value-from-get-posts-function/#post-7295845)
 * Thanks again for the response,
 * We ran a few queries on the database looking for duplicate store names and duplicate
   post id’s but found none. I even downloaded a csv of the postmeta table and filtered
   in in excel.
 * Is there perhaps a fool-proof way I can query this? Just to make double sure?
 * I’ve also thought of using a DB Cleaner Plugin, but there are very few and they
   come with their risks. I’m not sure if you know of a safe and reliable one?
 * I’m sorry to ask, but could you possibly explain your third paragraph a little
   more?
 * (Values should be saved with add_post_meta($post->ID, ‘wholly_owned/member’, 
   $value, true);, or better yet, use update_post_meta(). If update is used where
   no key/value yet exists, it is added. The resulting code is less complicated 
   and less prone to undesired multiple values.)
 * Thank you so much for the time you have taken to help with this.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/count-function-returning-incorrect-value-from-get-posts-function/#post-7295860)
 * It doesn’t sound like I’ve been much help in guessing at the cause 🙁
 * You could go into phpMyAdmin to look directly at the post meta table. Search 
   for the “wholly_owned/member” key. In the results, order by post_ID, then scan
   for a repeated ID. Pay particular attention to the ID before/after a page break
   for repetition, a repetition there can be easy to miss.
 * The third paragraph is suggesting the proper ways to save a meta value to avoid
   repetition, assuming the method in paragraph 2 was used. But if there is no repetition,
   then one of the proper ways was likely used. If there is no repetition, I’m unable
   to think of any other explanation for the behavior you describe.
 *  Thread Starter [MelmoSA](https://wordpress.org/support/users/melmosa/)
 * (@melmosa)
 * [10 years ago](https://wordpress.org/support/topic/count-function-returning-incorrect-value-from-get-posts-function/#post-7295938)
 * For everyone’s information, I eventually discovered the cause of my issue:
 * Basically, I had activated my Advanced Custom Fields Filed Group by selecting
   on of my Store Locator categories, but then deselected that category as I did
   not intend to add a new store, I went on to create a whole different post entirely.
   I don’t think this would usually have been a problem, but because of the Radio
   Buttons in question, with “Wholly Owned” set as a Default, it immediately registered
   in the database when I saved the post, and stayed there, even though the Field
   Group wasn’t in use within that post when I saved.
 * I found the offending post when I ran a Database Query, suddenly there was a 
   non-store in the list.
 * Hopefully this helps someone else in the future.
 * Thanks for all the help 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Count Function returning incorrect value from Get Posts Function’ is 
closed to new replies.

## Tags

 * [count](https://wordpress.org/support/topic-tag/count/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 2 participants
 * Last reply from: [MelmoSA](https://wordpress.org/support/users/melmosa/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/count-function-returning-incorrect-value-from-get-posts-function/#post-7295938)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
