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:
$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?
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…
Thanks for the help.
Try something like the below.
change your sql to :
it will have array of records on success to
print_r
after getting from database.