This should do what you want.
{% set videos = craft.entries({
section: 'articles',
type: 'video',
level: 2,
}) %}
{% set articleIds = [] %}
{% for video in videos %}
{% set article = video.getParent() %}
{% set articleIds = articleIds|merge([article.id]) %}
{% endfor %}
{% set articles = craft.entries({
id: articleIds,
fixedOrder: true,
limit: 6
}) %}
You go through all the videos and collect the IDs of the parent article entries. Once you have the IDs, make sure to set fixedOrder
in the final criteria model and the entries are retuned in the same order as the IDs in the id
param.
Note that the getParent
function queries the database for each video in your section. You might want to consider caching that piece of code.
And you can also test if replacing the code in the for loop with the following performs better.
{% set articleId = video.getAncestors(1).ids()[0] %}
{% set articleIds = articleIds|merge([articleId]) %}