php - Getting right value from $wpdb -


before creating new post programmatically want check if there post same headline in database.

i checking this:

$results = $wpdb->get_results("select count(*) $wpdb->posts post_type = 'candidates' , post_title = '$name'"); 

example of value when var_dump($results):

array(1) { [0]=> object(stdclass)#85 (1) { ["count(*)"]=> string(1) "8" } }

now want string(1) "8", add variable , convert number, not sure how it.

i tried

$number = (int)$results[0]->count(*)  

but isn't right. tried other combinations couldn't figure out

your problem (*) in $number = (int)$results[0]->count(*). cannot use these special characters 'variable'. you're better off using column alias using as your_column_alias.

so want use

select count(*) post_count $wpdb->posts  post_type = 'candidates' , post_title = '$name' 

as query ,

$number = (int) $results[0]->post_count; 

to fetch aliased column resultset.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -