Support » Fixing WordPress » Query if custom field value equals a current user value

  • As a result of the way we are creating posts, some will have a related user attached as a custom field value of that user’s WP username.

    I’m trying to create a loop, to be dropped into a page, that queries for all posts where the custom field value equals that of the currently logged in username.

    I’m at a loss as to how, or if its even possible, to ask WP to search for and compare these two variables.

    All help is very gratefully received.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Here is some sample code that should get you started:

    $paged = ($_GET['paged']) ? $_GET['paged'] :
       (($_GET['page']) ? $_GET['page'] : 1);
    $args = array(
       'posts_per_page' => 10,
       'paged' => $paged,
       'ignore_sticky_posts' => 1,
       'meta_key' => 'my-custom-field',  // Put your custom field name here
       'meta_value' => $current_user->user_login,
    );
    $myquery = new WP_QUERY($args);
    if ( $myquery->have_posts() ) {
       while ( $myquery->have_posts() ) {
          $myquery->the_post();
          // Put the code here to display the post
          echo '<p>'; the_title(); echo '</p>';
       }
    }
    wp_reset_query();

    Thank you, this works perfectly – really appreciate your help

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query if custom field value equals a current user value’ is closed to new replies.