0

In my template I have the user id variable pulled out of the URL and then I want to search through my entries for posts where that user id is included in a custom field with a users field type as well as the default "author" field.

Here's what I've got working for the author field, and I just want to include my custom "coAuthors" field type as well...

With a url like "/author/joyce"

{% set username = craft.request.segment(2) %}
{% set author = craft.users.username(username).first() %}
{% set entries = craft.entries.section('blog').authorId(author.id) %}

That works great, but how can I also include entries where that author id is set in the coAuthors custom field?

I can't even seem to find it separately. This turns up nothing

{% set entries = craft.entries.section('blog').search('coAuthors.username:author.id') %}
1

You would want to use the relatedTo() method of elementCriteriaModel.

{% set username = craft.request.segment(2) %}
{% set author = craft.users.username(username).first() %}
{% set entries = craft.entries.section('blog').relatedTo(author) %}

If you need both then you may need to merge the results.

{% set username = craft.request.segment(2) %}
{% set author = craft.users.username(username).first() %}
{% set authorEntries = craft.entries.section('blog').authorId(author.id).find() %}
{% set coAuthorEntries = craft.entries.section('blog').relatedTo(author).find() %}

{# merge authorEntries and coAuthorEntries #}
{% set entries = authorEntries|merge(coAuthorEntries) %}

Not tested. But should be close. Update: added .find() which will convert the elementCriteriaModel to an array so that the merge filter works properly.

| improve this answer | |

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.