1

I know this question has been asked a few times but no of the answers work.

I have a News channel with a Users field type as one of the layout fields.

I want to find all news entries related to user id X but the entries returned are wrong.

{# Get current member e.g URL /members/1/ #}
{% set currentMember = craft.users.id(craft.request.segment(2)).first() %}

{# Get current members news entries #}
{% set currentMemberNews = craft.entries.section("news").relatedTo(currentMember) %}

currentMemberNews contains all news. I just want the currentMember news.

Not sure what I am doing wrong?

3
  • {{ currentMember.email }} prints what you expect it to?
    – carlcs
    Dec 9 '14 at 8:51
  • Yes, the correct email address is printed out. Dec 9 '14 at 9:04
  • There are 7 news entries in the system with only 2 news entries tagged to the current member. I get all 7 news entries. Dec 9 '14 at 9:04
1

Have you tried this:

{% set currentMemberNews = craft.entries.section('news').relatedTo({
    targetElement: currentMember,
    field: 'yourFieldHandle',
}) %}

To specifically define the correct field and the fact that the member is on the target side of this relation?

8
  • You won by 8secs!
    – carlcs
    Dec 9 '14 at 9:10
  • Great minds think alike ;)
    – Paul
    Dec 9 '14 at 9:11
  • yourFieldhandle - do you mean the Users field handle in my News Channel ? Dec 9 '14 at 9:12
  • You define the field in which the relationship was initialized. In this case, the User field in your news channel. The big question, however ... does it work?
    – Paul
    Dec 9 '14 at 9:13
  • Okay, I've tried the above and I still get my 7 entries. :( Dec 9 '14 at 9:14
1

The problem seemed to be caused by a bad relation. Removing the relation and re-creating the relationship between the User and entries fixed the issue. Dunno what caused it but it was very strange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.