1

I would like to combine two sections: blog and recipes.

I need to filter the blog section by a field. I have an entries field on on Blog and I want to query those entries where the field is empty.

{% set posts = craft.entries({
   section: ['blog', 'recipes'],
   limit: 12,
   search:'-location:*'
}) %}

This will not return recipes. I think because the location field isn't present on recipes - only blog.

1
{% set blogIds = craft.entries({
   section: 'blog',
   search: '-location:*',
   limit: null
}).ids() %}

{% set recipeIds = craft.entries({
   section: 'recipes',
   limit: null
}).ids() %}

{% set entries = craft.entries({
   id: blogIds|merge(recipeIds),
   limit: 12,
})

Limit null is only there incase you're paginating, can be removed else.

This is the easiest way I've found for effectively combining element queries.

  • wouldn't i still be querying possibly hundreds of entries? – millman2394 Aug 12 at 17:27
  • You would be querying potentially hundreds of IDs (a single column from a single DB table, which is a fairly efficient query), however the end entry query has limit: 12 (the same as your question). Your other option is to query 12 of each and then sort them by postDate. Combining element queries with different criteria for different sections is not (as far as I'm aware) possible in craft – Nick Aug 13 at 7:55

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.