• 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?

Viewing 12 replies - 1 through 12 (of 12 total)
  • 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)";
    Thread Starter astromono

    (@astromono)

    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!

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

    Thread Starter astromono

    (@astromono)

    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 https://www.astromono.com/staff

    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):
    ?>
    Thread Starter astromono

    (@astromono)

    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.

    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):
    ?>
    Thread Starter astromono

    (@astromono)

    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.

    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):
    ?>
    Thread Starter astromono

    (@astromono)

    Amaziiiing!

    Dude, check it out! https://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):
    ?>`

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

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

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Filtering information with get_results’ is closed to new replies.