Answers.Body: <p>MikeLab, Welcome to CiviCRM SE. </p>
  
  <p>The address to be displayed on search result (primary or non-primary) is based on the setting 'Search Primary Details Only'.</p>
  
  <p>You can change the value of this setting by navigating to CiviCRM >> Administrator >> Customize Data and Screens >> Search Preferences. </p>
  
  <p>If you set 'Search Primary Details Only' to yes then it search address or phone or email on primary, if no then it will search on all address or phone or email. The same logic also works on listing.</p>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-23T16:15:17.973Z
Answers.Text: Oh, interesting Pradeep!  My client who pointed this out indeed has "Search Primary Details Only" turned off.  That makes a lot of sense based on my answer below, and explains why I couldn't replicate this in testing.  I suppose the solution is some sort of `ORDER BY` statement.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-23T20:22:25.313Z
Answers.Text: One of my users brought this to my attention and the contact record in question has several addresses.  There is an address marked primary, but that's not the address shown in the search result.  I believe the setting you're referring to determines if non-primary information is searched, but I don't think it determines what information is displayed on the results screen.
Answers.UserDisplayName: MikeLab
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-23T21:01:38.497Z
Answers.Text: I think so it does ;)
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-23T15:51:30.633Z
Answers.LastActivityDate: 2019-07-23T15:51:30.633Z
Answers.UserDisplayName: Pradeep Nayak
Answers.Body: <p>A client recently pointed this out to me, and I started to trace it down, but couldn't replicate it in my simple tests, so I just gave them a custom profile.  However, I found the place in the code the query is generated:</p>
  
  <ul>
  <li><code>CRM_Contact_BAO_Query</code> is where all the magic happens.  In the <code>initialize()</code> method there's this line:
  <code>$this-&gt;_fromClause = self::fromClause($this-&gt;_tables, NULL, NULL, $this-&gt;_primaryLocation, $this-&gt;_mode, $apiEntity);</code></li>
  <li>After that line runs, <code>$this-&gt;_fromClause</code> is set.  In my testing, that includes <code>LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )</code>.  This is the correct behavior, not what we're seeing.</li>
  <li>I tried figuring out how to trigger it NOT getting the <code>is_primary = 1</code> bit.  This is where I ran out of steam.  I'm 90% sure it's in <code>getEntitySpecificJoins()</code>, but it could be related to <code>getPrimaryCondition()</code>.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-23T20:23:15.233Z
Answers.Text: OK.  Thanks for giving me a place to start.  I'll take a closer look later this evening.
Answers.UserDisplayName: MikeLab
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-24T13:21:46.290Z
Answers.Text: The problem is that there are multiple joined address records and only the first one is displayed.  A way to fix this would be to sort the address records (or emails or phones) by is_primary DESC, so the primary records show up first and are displayed. I have looked at the code for several hours, but can't figure out where I would add that.  Do you have any idea where I could add an "order by" to sort the results so the primary address is returned first?
Answers.UserDisplayName: MikeLab
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-24T16:04:08.563Z
Answers.Text: It's a good question.  As you probably saw, the query is put together from its various clauses (`$this->_select`, `$this->_from`, etc.).  I think we'd need to define a `$this->_orderBy`.  There's precedent for this in CiviReport (in the `CRM_Report_Form` class) but it seems like a lot of work for relatively little payoff. Come talk to me on chat.civicrm.org (I'm @junglebird) if you want to tackle it, I can probably put in some effort on this.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-23T16:13:48.667Z
Answers.LastActivityDate: 2019-07-23T16:13:48.667Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>When I do Search->Find Contacts and get the standard list of contacts, it looks like the address displayed is not the contact's primary address, but simply the first address that was created for the contact.  I've looked in the code to try to find where this query is located, but I can't find it.  Shouldn't the search results screen display the primary address, primary email, and primary phone?</p>
  
  <p>I know I can create a custom profile to change the search results screen, but I would think that the default screen should display the primary contact information by default.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-24T00:01:45.850Z
Comments.Text: Steps to reproduce:  create a new contact with address "111 First St".  Add a second address "222 Second St" and make it primary.  Turn off "Search Primary Details Only".  Now you can search for First St or Second St, but First St will always show up in the search results because it has a lower address ID and apparently the address records sort by record ID and only the first one is displayed on the screen.
Comments.UserDisplayName: MikeLab
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-09-05T19:04:56.377Z
Comments.Text: This also causes exports of address fields with type primary to return erroneous results. If you have "Search Primary Details Only" set to off, your export with primary address fields will actually give you the first-created address fields, not the primary ones:
  https://civicrm.stackexchange.com/questions/31936/exporting-primary-addresses-exports-non-primary-addresses-for-some-contacts/31949#31949
Comments.UserDisplayName: Lars SG
Tags: <search-results>
Title: Contact Search Results Address Type
ThreadText: # Contact Search Results Address Type
  
  (Asked by: username_0 on 2019-07-23)
  
  When I do Search->Find Contacts and get the standard list of contacts, it looks like the address displayed is not the contact's primary address, but simply the first address that was created for the contact. I've looked in the code to try to find where this query is located, but I can't find it. Shouldn't the search results screen display the primary address, primary email, and primary phone?
  
  I know I can create a custom profile to change the search results screen, but I would think that the default screen should display the primary contact information by default.
  
  **Tags:** search-results
  
  > **Comments:**
  > **username_0**: Steps to reproduce:  create a new contact with address "111 First St".  Add a second address "222 Second St" and make it primary.  Turn off "Search Primary Details Only".  Now you can search for First St or Second St, but First St will always show up in the search results because it has a lower address ID and apparently the address records sort by record ID and only the first one is displayed on the screen.  
  > **username_1**: This also causes exports of address fields with type primary to return erroneous results. If you have "Search Primary Details Only" set to off, your export with primary address fields will actually give you the first-created address fields, not the primary ones: https://civicrm.stackexchange.com/questions/31936/exporting-primary-addresses-exports-non-primary-addresses-for-some-contacts/31949#31949  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2019-07-23. Score: 4
  
  username_0, Welcome to CiviCRM SE.
  
  The address to be displayed on search result (primary or non-primary) is based on the setting 'Search Primary Details Only'.
  
  You can change the value of this setting by navigating to CiviCRM >> Administrator >> Customize Data and Screens >> Search Preferences.
  
  If you set 'Search Primary Details Only' to yes then it search address or phone or email on primary, if no then it will search on all address or phone or email. The same logic also works on listing.
  
  HTH
  
  username_2
  
  > **Comments:**
  > **username_3**: Oh, interesting username_2!  My client who pointed this out indeed has "Search Primary Details Only" turned off.  That makes a lot of sense based on my answer below, and explains why I couldn't replicate this in testing.  I suppose the solution is some sort of `ORDER BY` statement.  
  > **username_0**: One of my users brought this to my attention and the contact record in question has several addresses.  There is an address marked primary, but that's not the address shown in the search result.  I believe the setting you're referring to determines if non-primary information is searched, but I don't think it determines what information is displayed on the results screen.  
  > **username_2**: I think so it does ;)  
  
  ---
  
  ### Answer by username_3 on 2019-07-23. Score: 1
  
  A client recently pointed this out to me, and I started to trace it down, but couldn't replicate it in my simple tests, so I just gave them a custom profile. However, I found the place in the code the query is generated:
  
  * `CRM_Contact_BAO_Query` is where all the magic happens. In the `initialize()` method there's this line:
    `$this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode, $apiEntity);`
  * After that line runs, `$this->_fromClause` is set. In my testing, that includes `LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )`. This is the correct behavior, not what we're seeing.
  * I tried figuring out how to trigger it NOT getting the `is_primary = 1` bit. This is where I ran out of steam. I'm 90% sure it's in `getEntitySpecificJoins()`, but it could be related to `getPrimaryCondition()`.
  
  > **Comments:**
  > **username_0**: OK.  Thanks for giving me a place to start.  I'll take a closer look later this evening.  
  > **username_0**: The problem is that there are multiple joined address records and only the first one is displayed.  A way to fix this would be to sort the address records (or emails or phones) by is_primary DESC, so the primary records show up first and are displayed. I have looked at the code for several hours, but can't figure out where I would add that.  Do you have any idea where I could add an "order by" to sort the results so the primary address is returned first?  
  > **username_3**: It's a good question.  As you probably saw, the query is put together from its various clauses (`$this->_select`, `$this->_from`, etc.).  I think we'd need to define a `$this->_orderBy`.  There's precedent for this in CiviReport (in the `CRM_Report_Form` class) but it seems like a lot of work for relatively little payoff. Come talk to me on chat.civicrm.org (I'm @junglebird) if you want to tackle it, I can probably put in some effort on this.  
  
  ---
  
  