How to get the page_ids from a list of page_slugs from WordPress and store them as a string?

What I want to do is create a wordpress function to get the page_ids from a list of page_slugs and store them as a string in a variable which I can then simply “echo” / “call” from inside another function.

So far, I can get a single page_id from a page_slug with this:

Read More
$the_page_slug = 'test';
global $wpdb;
$page_id_from_slug = ''' . $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name ='".$the_page_slug."'") . ''';

echo $page_id_from_slug;

result is ok: '12'

and this function works too

function id_from_slug($the_page_slug){
global $wpdb;
$page_id_from_slug = ''' . $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name ='".$the_page_slug."'") . ''';
return $page_id_from_slug;
}
echo id_from_slug('test');

result is ok: '12'

The problem is that I cant get it to work with an array of page_slugs.

My Data is like this:

$the_page_slugs = array('test', 'test-nr4', 'test-9', 'sample4', 'sample-nr12');

the echo output (page_ids) should come out like this:

’12’, ’16’, ’54’, ’76’, ‘123’

any ideas?

Related posts

Leave a Reply

3 comments

  1. So here is my solution, works fine, just how I need it.
    Look at the code and see what I changed/added:

    The IN() and IMPLODE() hint was really what I needed and after changing “get_vars” (only gets ONE result) to “get_results” and a bit tweaking it worked.

    Here is my code, for sure it can be improved…

            // code to get page_ids from page_slugs
            // the list of page_slugs
    $the_page_slugs = array('test', 'test-re', '1234', 'test-56', 'sample34', 'me3-uc', '1-12987-db', 'thy-m2o-1873');
            // set global, just in case ;)
    global $wpdb;
            // query to get the page_ids
    $sql = "SELECT ID FROM wp_posts WHERE post_name IN('" . implode("', '", $the_page_slugs) . "') AND post_status = 'publish'";
            //this calls the query
    $get_page_ids = $wpdb->get_results($wpdb->prepare($sql));
            //prepare new var
    $the_page_ids ='';
            //loop thru results to create var
    foreach($get_page_ids as $get_page_ids_result){
            //formate results, need it comma separated
        $the_page_ids .= ('' == $the_page_ids ) ? '':', ';
            //fill it
        $the_page_ids .= $get_page_ids_result->ID;
        }
    echo "check if result is what we need<br/>";
    echo $the_page_ids;
            //perfekt result, looks like: 325, 323, 324, 327, 328, 329, 334, 335
    

    Thanks for the help.

  2. Try something like the below.

    global $wpdb;
    
    $the_page_slugs = array('test', 'test-nr4', 'test-9', 'sample4', 'sample-nr12');
    $page_id_from_slug = ''' . $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name in '" . implode("','",$the_page_slugs) . "'") . ''';
    
    echo $page_id_from_slug;
    
  3. change your sql to :

    "SELECT ID FROM $wpdb->posts WHERE post_name in (".implode(",",$the_page_slugs)."))";
    

    it will have array of records on success to print_r after getting from database.