I need some guidance on making a wordpress posts query responsive to user meta data, and whether it’s possible to do what I want within the query itself, or if further work is needed in the database first?

Every user on my site has a meta field with the meta key language_level. These broadly correspond to beginner, intermediate, advanced.

The post-login homepage of my site has a feed of recommended content. This is composed of posts tagged with a custom taxonomy, Recommended Resource, with tags that correspond to those user levels. Ie Beginner recommendations, Intermediate recommendations etc.

The goal is to have a posts listing where:

  • Beginner users see posts tagged as beginner
  • Intermediate users see posts tagged as intermediate
  • Advanced users see posts tagged as advanced

I’m currently using the query below in my functions to pull through all posts….

add_action( 'elementor/query/article_video_together', function( $query ) {
$query->set( 'post_type', [ 'article', 'video' ] );
} );

… can I achieve my goal through a single query, adapted from above or does there need to be a relationship between the meta field and the taxonomy first?

I've asked this question in several places without getting a clear response, so I'm wondering if it's more complicated than it appears? Any guidance much appreciated.

  • hmm that filters is not a WordPress filter, but an Elementor filter, have you asked Elementor support or posted in an Elementor community? It's very probably that the answer to this question will not be useful to you because of your use of 3rd party plugins to execute and filter the query
    – Tom J Nowell
    Apr 7, 2021 at 20:57
  • otherwise it's unclear which part you're having trouble with, e.g. do you know how to retrieve this user meta value? If so, what prevents a set of if/else blocks that conditionally change the query? Where did you ask this previously?
    – Tom J Nowell
    Apr 7, 2021 at 20:59
  • @TomJNowell Thanks that helps. I was confused because I'm confident with SQL but untrained with PHP. I had thought it would be as you state: find current user, retrieve meta, use if else to amend query, (much as you use case when in SQL), but the few php developers I'd approached were giving non-commital answers, as if there was something bigger I didn't understand. Apr 8, 2021 at 9:45
  • "otherwise it's unclear which part you're having trouble with..." that part would be the syntax of wrapping the mySQL query into the PHP query. I can write the mySQL code easily enough, but how to put into that query is the bit I don't understand. Apr 8, 2021 at 10:10
  • 1
    I do not see any need for SQL to be involved in this, it is unrelated. You already have access to the WP_Query object, and you know how to retrieve the user meta, so why not just tell it which post tag you want based on the user meta value? Remember, WP_Query is not SQL, ok up the WP_Query documentation and you'll see all the parameters it accepts, including post tags. If you end up writing a single line of SQL trying to solve this then you've made a terrible mistake. To clarify, you should be doing this in PHP, not SQL.
    – Tom J Nowell
    Apr 8, 2021 at 11:13

2 Answers 2


You would not do this using SQL, and you don't need to. Use PHP instead.

For example, this code adds a search parameter to the query, but the value changes depending if the user is logged in:

add_action( 'elementor/query/article_video_together', function( \WP_Query $query ) {
    $foo = is_user_logged_in();
    if ( true === $foo ) {
        $query->set( 's', 'logged in search term' );
    } else {
        $query->set( 's', 'logged out search term' );
    $query->set( 'post_type', [ 'article', 'video' ] );
} );

You can do the same using get_user_meta and the post tag parameters of WP_Query.

Note that this answer was written assuming that elementor/query/article_video_together works the same was as the pre_get_posts action. If it does not, then you need to go to Elementor support or ask in an Elementor community

  • Thank you! You just handed me a map. I will give this a try on my staging environment. Huge thanks again! Apr 8, 2021 at 12:02

I finally got a chnace to play with @tomjnowell suggestion yesterday, and I ended up with the code below, which works:

add_action( 'elementor/query/user_responsive1', function( $query ){
$language_level = get_user_meta(get_current_user_id(),'language_level',true);
    if ($language_level == 'A1' or $language_level == 'A2'){
        $query->set( 'recommended_resource', 'A2 Recommendations' );
    } elseif ($language_level == 'B1') {
        $query->set( 'recommended_resource', 'B1 Recommendations' );
    } elseif ($language_level == 'B2' or $language_level == 'C1 or better'){
        $query->set( 'recommended_resource', 'B2 Recommendations' );
    } else {
        $query->set( 'recommended_resource', 'A2 Recommendations' );
    } $query->set( 'post_type', [ 'article', 'video' ] );

Tom was right - not a single line of SQL required, php was more than adequate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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