Just to clarify the get_var()
method of $wpdb
does work just fine in this context:
global $wpdb;
$helloworld_id = $wpdb->get_var("SELECT ID FROM wp_posts WHERE post_name="hello-world"");
echo $helloworld_id;
Actually it is more practical in this context, because a single variable is returned, which is what is actually wanted.
Related Posts:
- How to make an activities stream mixing posts and comments?
- How to correctly pass values to wpdb->prepare()?
- Is it possible to query from external database? [duplicate]
- List of ways to access WordPress database?
- Any way to use FETCH_KEY_PAIR with $wpdb?
- Include post_status check within $wpdb query
- wpdb get_results() returns only 2 rows
- Pagination with custom SQL query
- Return only Count from a wp_query request?
- Export wordpress table to excel
- What is an efficient way to query based on post_meta?
- SQL query equivalent to WP User Query
- how to retrieve specific product attribute value in an sql query?
- WP_Query Performance Issues with meta_query
- Should close $wpdb via $wpdb->close()
- Get the post permalink within the loop but without additional DB query
- Filter WordPress posts by between parameter
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- Select from wp_post and multiple meta_value from wp_postmeta
- Order by custom table and multiplication
- $wpdb select date range of posts
- List of the years with posts presented
- Optional Meta Query
- Custom Query num_rows returns wrong amount
- Editing the default wordpress search
- Iterating through $wpdb query without using get_results for large query results
- WP Query related posts by tags
- Order by summing multiple values
- wp_Qwery works to slow
- Include data from custom table in WP_Query
- No results found from a $wpdb->get_results() query when trying to join more than one meta key query
- How to access custom WordPress database tables
- How to pass orderby params to $wpdb->prepare()?
- Make a SQL query with wpdb in WordPress
- WordPress query posts with multiple post_meta data
- Specify strict ‘order by’ in WordPress query
- How to set up hierarchical relationships without using plugins / meta query
- Does WordPress have something like Drupal’s DB API?
- Query where ANDing slug values not working
- Slow Query On Search
- How to SQL query posts IDs by categories AND authors?
- Disable (or limit) queries when certain content (or data) is not needed (or showed)
- Passing a SQL query to the WP Query
- Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value
- How to get meta key list efficiently?
- How to get user avatar via WPDB
- Translating WP query into to SQL query
- Slow wp_posts and wp_postmeta query using Advance Custom Fields
- What argument does my function need to echo get_results() query results
- WP_Query always returning the last custom post
- Optimising amount of calls to custom fields
- wpdb LIKE request shows all database data
- Convert a WP Query into a simpler SQL query to fetch only COUNT of posts
- How much does $wpdb->prepare(), then $wpdb->query() VS straight $wpdb->query(), can slow down the load time of whole page
- how to use transient method?
- WP_Query with “post_title LIKE ‘something%'”?
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- How to uniquely identify queries?
- Multipart/formatted MySQL query problem
- Use WP_Query with a custom SQL query
- How to query for a week using key => value WP_Query argument notation?
- meta_query on a date range using an array of values
- WPDB: how to get the value of a field in a custom database table
- How to detect custom query inside `posts_where` hook?
- Is temporarily overwriting $wp_query a bad idea?
- How to output different posts per page?
- Mysql query LIKE not working
- How to orderby meta_value_num with dollar ($) sign
- Query: offset post list, unless it’s a specific category
- Complex WP SQL Query
- Advanced SELECT query with condtional statements
- How to create database table, add data, update and delete using wpdb via plugins?
- Sorting events by descending date, and ascending time if multiple events on a date
- WP_Query with ‘rand’, but equal number of posts from the taxonomy terms given
- querying data via $wpdb and get_row
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Sort WordPress Posts Meta value by Week not Day
- 2 wordpress loops showing 1 post from same post type – how to avoid showing the same post?
- Conditional sorting with variable using WP Query
- Why Query is returning empty array?
- WP User Query with Custom Fields and Search Results
- meta_value timestamp older than now
- Clear Terms from Taxonomy for Specific Post IDs?
- How to filter query loop block with a search string from the query parameters
- Function to retrieve IDs of posts, cache results, and improve wp_query
- How to avoid filling up an array each time I run a WP_query?
- Query multiple post types, but different order for each
- What is the equivalent WP_Query of a SQL Query?
- Query against multiple locations within single custom post type post
- Fix wp_term_relationships slow query in get_posts
- How to get_comments() ordered by date and parent?
- Adding Category in WP_Query Not Working
- Merge two queries and remove duplicate
- Fetch only categorized posts
- Custom pagination (Title, date and teaser)
- Fire query on ajax post url page
- Wpdb generates too many queries
- Showing posts from 4 categories along with all latest posts
- how to add limit records in wordpress query
- WP_Query by meta key not returning any posts