The main problem is how you’re specifying the custom table name (e.g., $wpdb->cf7dbplugin_submits
).
$wpdb
only knows about the “built-in” tables when accessing a table name via $wpdb->xxx
. To specify access a custom table name, use {$wpdb->prefix}custom_table_name
.
The other thing I notice is that, for security purposes, you should never use interpolated variables
in an SQL statement passed to any of the $wpdb
query methods. Instead, you should use $wpdb->prepare().
Putting these 2 things together results in:
$sql = $wpdb->prepare (
"SELECT min(TIMESTAMPDIFF($day,`fecha_inscripcion`, $now))
FROM {$wpdb->prefix}cf7dbplugin_submits
WHERE field_name=%s and field_value=%s",
$fieldname,
$fieldvalue
) ;
$post_count = $wpdb->get_var ($sql) ;
Related Posts:
- What argument does my function need to echo get_results() query results
- Pagination with custom SQL query
- How to make an activities stream mixing posts and comments?
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- Select from wp_post and multiple meta_value from wp_postmeta
- How to correctly pass values to wpdb->prepare()?
- $wpdb select date range of posts
- How to delete taxonomy term when a wordpress user is removed?
- How to pass orderby params to $wpdb->prepare()?
- List of ways to access WordPress database?
- Passing a SQL query to the WP Query
- Any way to use FETCH_KEY_PAIR with $wpdb?
- Include post_status check within $wpdb query
- wpdb LIKE request shows all database data
- Convert a WP Query into a simpler SQL query to fetch only COUNT of posts
- how does $wpdb differ to WP_Query?
- Is it possible to wrap Geo Location search around WP_Query?
- ajax category filter
- Is it possible to select against a post’s parent’s fields with WP_Query?
- WordPress 4.9.5 PHP intermittent warning trim() expects parameter 1 to be string, array given
- Query causing load because of SQL_CALC_FOUND_ROWS post counting?
- SQL Statement generated by WP_Query not producing expected results
- What is an efficient way to query based on post_meta?
- How can I connect to another WP database and use WP_Query?
- Recommended way to drop a pending query (in pre_get_posts)?
- How to get any tag ID
- how to retrieve specific product attribute value in an sql query?
- Get posts using WP_Query
- Should close $wpdb via $wpdb->close()
- Finding post content that begins with a specific character
- SQL User Query by Multiple Roles using PHP
- Advanced WP Query hogs the SQL server
- Best practice custom function, where to echo the variables?
- How to get user_id from wordpress database inside ajax function?
- Get the Plugin Which Triggered a MySql Query in WordPress?
- Mysql query LIKE not working
- WP User Query get all authors with last name starting with specific letter
- Exclude admin from WP_Query Contributors
- Order by custom table and multiplication
- Multiple Loops Inside a Function
- Sort users by “birthday” using WP_User_Query
- What’s the purpose / logic of split_the_query in WP_Query->get_posts
- WPDB query – decrypting DB data
- SQL: What is wrong with the following query (generated by WordPress WP_Query, ordering prices)
- Slow WP_Query for custom post type
- Display posts from only one post form in custom query and exclude in main query
- Complex WP SQL Query
- Custom Query num_rows returns wrong amount
- Sorting events by descending date, and ascending time if multiple events on a date
- Filtering posts from different categories into different section by doing WP_Query only once
- Category applied to pages, creates multiple breadcrumb entries after a search query (On the translated site)
- Order by summing multiple values
- wordpress search word, “hello world” ===> ‘hello+world’ ===> ‘hello’, ‘world’
- meta_query not working as expected
- wordpress ajax relationship query
- Custom query: Get all posts that are from a certain category?
- Ajax future single post query doesn’t work when NOT logged in
- Conditional sorting with variable using WP Query
- Debugging wp_query orderby for taxonomy
- Getting additional columns from sql
- WordPress query posts with multiple post_meta data
- how to use pre_gets_posts to exclude one queried ID from homepage loop
- get_post() is not returning correct value
- Loading two different AJAX requests on two different pages
- How to load a script code only in posts?
- Multiple meta_key ordering with pre_get_posts
- WP_Query: apply an SQL function to meta fileld value
- Trying to get property of non-object in: $wp_query
- Randomly select one of last 3 posts
- WordPress SQL JOIN query
- Use have_posts() with array of post results retrieved by $wpdb->get_results
- how to translate countries output from wp database?
- duplicated posts when using pagination
- WP_Query() not working as expected
- Pagination repeating posts on search results
- sort title descending with title with number
- Delete oldest wordpress post (SQL query)
- How to check if logged in user have pending custom post?
- Custom WordPress post query for displaying time-released content on website
- How to get user avatar via WPDB
- Modify post image in full size
- How to upload 3 attachments to current post?
- In which file to use $wpdb and its functions for database operations and queries in wordpress?
- Get post Number with local loop and template
- complex query question
- Fetching $_POST from Page Template into functions.php
- wpdb get_results() returns only 2 rows
- Find by post_meta, then sort by post_meta, then sort by date (wp_posts)
- Optimising amount of calls to custom fields
- Hiding all posts/products/pages from a site based on a custom taxonomy/domain name
- pre_get_posts or $where, which one to use?
- WP Query to order posts by multiple meta fields
- Search Query that Includes Custom Table
- Why WP_Query in functions.php is not working when get_posts works?
- How to return value from sql and display it
- WP_Query with several meta_query-statements and order by meta_value
- 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?
- 1500+ duplicate queries via get_option function (query monitor)
- Add custom argument to WP_Query and modify SQL where clause