Sql query that shows all users with meta_key equals to 1 (5 posts)

  1. Nikolas
    Posted 6 years ago #

    I am asking for a query I would like to use inside my blog, that searches MySql.

    The query has to look inside the wp_usermeta, and check whether the new field I made with name "support" is equal to 1.

    After that it will display all the users that have the field support equal to meta_key 1.

    Thanks in advance.

  2. Nikolas
    Posted 6 years ago #

    maybe this will help someone?

     $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'tag'
        AND wpostmeta.meta_value = 'email'
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'post'
        AND wposts.post_date < NOW()
        ORDER BY wposts.post_date DESC
     $pageposts = $wpdb->get_results($querystr, OBJECT);


    //get all users, iterate through users, query for one post for the user, if there is a post then display posts title, author, content info
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $args = array(
        'author' => $bloguser->user_id,
    	  'showposts' => 1,
    	  'caller_get_posts' => 1
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          // $user = get_userdata($bloguser->user_id);
          // echo 'This is one post for author with User ID: ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?> </small><?php
  3. Mark / t31os

    Posted 6 years ago #

    Your first code suggestion is the better of the two imo. Nice one posting back your solution(s).. ;)

  4. Nikolas
    Posted 6 years ago #

    Hello t31os_

    thank you for your reply.

    Well this code ( the first one ) searches posts am I right?

    I know about the AND, OR stuff, it says to display wposts by it's day where some tags are available.

    But how to turn it and look the authors instead of posts?

  5. Nikolas
    Posted 6 years ago #

    .. and after this, it will create a new field that contains all the id's that meet the requirements

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.