Viewing 9 replies - 1 through 9 (of 9 total)
  • To get that sort of information, you’d need to create your own custom database query, but it’s not that complicated. Something like this (completely untested) might work…

    global $wpdb;
    
    $query = "SELECT meta_key, meta_value FROM ".$wpdb->posts." WHERE meta_key = 'email'";
    
    $email_list = array ();
    
    foreach ($wpdb->get_results ($query) as $row) {
        $email_list [] = $row->meta_value;
    }
    
    $emails = implode (',', $email_list);

    The only thing that I’d suggest is to not do this for the purpose that you’ve stated. The reason for this is that the vast majority of hosting companies don’t like it when you bulk-send emails from their servers as that’s the no. 1 way that SPAM is sent, so they can be very harsh with it. At best, you’ll have alot of emails failsilently because you’ve gone over sending limits, and at worst your account will be shut down. There’s other services around that can do bulk sending, and I would really recommend you to look into that.

    Thread Starter harshclimate

    (@harshclimate)

    Good point on the mass email. Didn’t even think about that. I’ll give your snippet a try and see what happens ??

    Thread Starter harshclimate

    (@harshclimate)

    That snippet isn’t showing any info. The wpdb variable… do I have to put my own database name there? Or is that something that doesn’t care about database names?

    The $wpdb varlable is the standard WordPress database connection object that’s used throughout the system. https://codex.www.ads-software.com/Class_Reference/wpdb You odn’t need to do anything more to use it as it’s set up from your wp-config.php file.

    As for the code, if it’s not showing anything you’ll need to do some basic de-bugging to find out what’s wrong. As I said, it’s not tested, so I can’t be sure if it’s going to work on your system. There’s a few points that could be wrong, but I’d mainly be looking at the meta datas name so that you’re sure that’s correct. You can also run through any error messages passed back from the query, and see what each row of the result set looks like.

    Oh… But I did make one mistake… The query line should be this:

    $query = "SELECT meta_key, meta_value FROM ".$wpdb->postmeta." WHERE meta_key = 'email'";

    I should look at these things more sometimes. ??

    Thread Starter harshclimate

    (@harshclimate)

    Thanks, @catacaustic! It’s working but not in order. Can you amend it in such a way that it orders by post_type? Here’s what I have so far:

    <?php while ( have_posts() ) : the_post(); ?>
    
                <div class="col-12">
    
                    <?php
                        global $wpdb;
    
                        $query = "SELECT meta_key, meta_value FROM ".$wpdb->postmeta." WHERE meta_key = 'email'";
    
                        $email_list = array ();
    
                        foreach ($wpdb->get_results ($query) as $row) {
                            $email_list [] = $row->meta_value;
                        }
    
                        echo '<ul><li>' . $emails = implode ('</li><li>', $email_list) . '</li></ul>';
                    ?>
    
                </div>
    
            <?php endwhile; ?>

    That’s a bit of a stretch… it’s a much more complicated SQL query as the post_type is held in the ‘posts’ table, not the ‘postmeta’ table. But… something like this might work…

    $query = "SELECT
        pm.meta_key AS meta_key,
        pm.meta_value AS meta_value
    FROM ".$wpdb->postmeta." AS pm
    INNER JOIN ".$wpdb->posts." AS p
        ON p.ID = pm.post_id
    WHERE pm.meta_key = 'email'
    ORDER BY p.post_type ASC";
    Thread Starter harshclimate

    (@harshclimate)

    Okay, that does work! I’m wondering (just cuz I’m genius at this kinda stuff) would I be able to use the previous query with custom wp_query in the loop to get the results I would need?

    So like:

    $args = array(
         'post_type' => 'wineries',
         'orderby' => 'rand',
         'posts_per_page' => 1
    ); $email = new WP_Query($args);
    
    <?php while ( $email->have_posts() ) : $email->the_post(); ?>
       
       global $wpdb;
       $query = "SELECT meta_key, meta_value FROM ".$wpdb->postmeta." WHERE meta_key = 'email'";
    
       $email_list = array ();
    
          foreach ($wpdb->get_results ($query) as $row) {
            $email_list [] = $row->meta_value;
          }
          echo '<ul><li>' . $emails = implode ('</li><li>', $email_list) . '</li></ul>';
    
    <?php endwhile; ?>

    No. You’d need to set up a proper WP_Query query object and use that. There’s a whole lot of options that will do pretty much what you’re after, but it may be slower than doing it custom, depending on what you have to od in the loop of course.

    Just read through the documentation and you’ll see what you can, and can’t, do.

    https://codex.www.ads-software.com/Class_Reference/WP_Query

    Thread Starter harshclimate

    (@harshclimate)

    Thanks so much, catacaustic! I’m just thrilled to get the emails where I can see them. One of the main reasons that I need them is to be able to email asking if any of their information needs to be updated. If you have a sec, take a look at what I’ve done: https://www.azbottle.com. I built this theme from the ground up from answers from ppl like you on these forums.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Grab all key/values from custom fields’ is closed to new replies.