7

I have a query that I need to do that involves using the logged in member ID to limit the results. The following does not work, because the {member_id} variable is not parsed early enough.

{exp:query sql="SELECT ct.title AS title
            FROM exp_channel_titles AS ct
            WHERE ct.member_id = {member_id};"
                }

I've tried to pass {member_id} as an embed as well, but it outputs the actual curly braces again and does not work either:

{embed="members/.favorite-query" member_id="{member_id}"}

{exp:query sql="SELECT ct.title AS title
            FROM exp_channel_titles AS ct
            WHERE ct.member_id = {embed:member_id};"
                }

I then tried to create a global variable using the session_end hook. If I hard code the global variable, the query works, but it turns out that the $this->EE->session array is not available yet when using the session end hook, so I cannot use that method dynamically.

There is a thread on this topic here: http://ellislab.com/forums/viewthread/168569/#804745

I also tried creating a member_id value using a module tag, but again, the parse order is against me.

Am I overlooking any other options available here?

8

You can use the {exp:member:custom_profile_data} tag to access the member's profile information. By default it will use the currently logged-in member.

http://ellislab.com/expressionengine/user-guide/modules/member/index.html#custom-profile-data-tag

As this is a module tag this should now parse the {member_id} variable earlier than the {exp:query} tag:

{exp:member:custom_profile_data}

    {exp:query 
        sql="SELECT ct.title AS title
        FROM exp_channel_titles AS ct
        WHERE ct.author_id = '{member_id}'"
    }
    <p>{title}</p>
    {/exp:query}

{/exp:member:custom_profile_data}
| improve this answer | |
  • I went with this option as I always enjoy having less addons around if possible. However, which of these methods do you think would have more overhead? If Mo' variables is just creating a global that seems simpler than running through the whole custom_profile_data tag. – Ben Parizek Dec 7 '12 at 19:23
  • It looks like the custom_profile_data runs 2/3 SQL queries, so probably has more overhead. I think Mo' variables gets everything from the existing session so should be quicker. Mo' variables will run on every page load, though, so it might depend whether you'd use it elsewhere or not. – Andrew Weaver Dec 7 '12 at 21:05
4

Mo' Variables adds early parsed member variables. Once you've installed that the following query should work (you want to use author_id for the where condition, not member_id).

{exp:query sql="
    SELECT title
    FROM exp_channel_titles
    WHERE author_id = '{logged_in_member_id}'"
}
| 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.