WordPress.org

Ready to get started?Download WordPress

Forums

Filtering information with get_results (13 posts)

  1. astromono
    Member
    Posted 3 years ago #

    Hi guys! Me again XD

    I'm trying to make a list of post authors, however I wish to order them by their ID numbers and also (if possible) exclude specific authors from the list.

    The working code I have is this:

    $authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts);

    It lists all authors by their respective IDs only the order is Descending (5,4,3,2,1) instead of Ascending. Can this be done?

  2. vtxyzzy
    Member
    Posted 3 years ago #

    Try this:

    $excluded = "1,33,4";  // To exclude IDs 1,33,4
    $authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts
    . " WHERE ID NOT IN ($excluded)
    ORDER BY ID ASC)";
  3. astromono
    Member
    Posted 3 years ago #

    Awesome! Thank you so much, it worked like a charm!

    The only thing was that I had to remove the ORDER BY ID ASC and add another parenthesis before the semi colon to wrap the thing.

    Strangely, the authors are still organized the way I wanted to, so I'm golden.

    Thank you!

  4. vtxyzzy
    Member
    Posted 3 years ago #

    Yes, that was a typo - the last paren should have been outside the last quote!

  5. astromono
    Member
    Posted 3 years ago #

    Hmm... for some reason the filtering is not working now that I applied the code to a live page!

    Hm... I'm trying to figure it out, but I don't know what it is.

    <?php
    		$excluded = "5,16,191,246,688,689";  // To exclude IDs 1,33,4
    		$authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts. " WHERE ID NOT IN ($excluded)");
    		if($authors):
    		foreach($authors as $author):
    		?>

    You can check it in action live here http://www.astromono.com/staff

  6. vtxyzzy
    Member
    Posted 3 years ago #

    It is possible that you need to declare $wpdb a global:

    global $wpdb;
    $excluded = "5,16,191,246,688,689";  // To exclude IDs 1,33,4
    $authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts. " WHERE ID NOT IN ($excluded)");
    if($authors):
       foreach($authors as $author):
    ?>
  7. astromono
    Member
    Posted 3 years ago #

    Thank you for the reply!

    I tried declaring wpdb as global, but still no go. I even tried adding the <?php opening, but it doesn't seem to be working.

  8. vtxyzzy
    Member
    Posted 3 years ago #

    OK - time for a little debugging. See what this will display:

    <?php
    global $wpdb;
    $excluded = "5,16,191,246,688,689";  // To exclude IDs 1,33,4
    $sql = 'SELECT DISTINCT post_author FROM '.$wpdb->posts. " WHERE ID NOT IN ($excluded)";
    print_r('<p>SQL: ' . $sql . '</p>');
    $authors = $wpdb->get_results($sql);
    if($authors):
       foreach($authors as $author):
    ?>
  9. astromono
    Member
    Posted 3 years ago #

    Thanks! I tried the new coding, but I still get all the authors posted, and this line of code jumped to the actual site:

    SQL: SELECT DISTINCT post_author FROM wp_posts WHERE ID NOT IN (5,16,191,246,688,689)

    The line appears right before the get_results prints all auhors.

  10. vtxyzzy
    Member
    Posted 3 years ago #

    I am so sorry, I set up the sql to exclude post IDs, not author ids. Here is the corrected code:

    <?php
    $excluded = "5,16,191,246,688,689";  // To exclude IDs 1,33,4
    $sql = 'SELECT DISTINCT post_author FROM '.$wpdb->posts. " WHERE post_author NOT IN ($excluded)";
    $authors = $wpdb->get_results($sql);
    if($authors):
       foreach($authors as $author):
    ?>
  11. astromono
    Member
    Posted 3 years ago #

    Amaziiiing!

    Dude, check it out! http://www.astromono.com/staff

    I even managed to slap on the ORDER BY ASC from the previous code and it works!

    `<?php
    $excluded = "5,16,191,246,688,689"; // To exclude IDs 1,33,4
    $sql = 'SELECT DISTINCT post_author FROM '.$wpdb->posts. " WHERE post_author NOT IN ($excluded) ORDER BY ID ASC";
    $authors = $wpdb->get_results($sql);
    if($authors):
    foreach($authors as $author):
    ?>`

  12. vtxyzzy
    Member
    Posted 3 years ago #

    Great! Now, please use the dropdown at top right to mark this topic 'Resolved'.

  13. cezar
    Member
    Posted 3 years ago #

    is there a way to get the authors displayed from A to Z by nickname? mine come randomly it seems

Topic Closed

This topic has been closed to new replies.

About this Topic