I don’t think there is a built in mechanism for searching the meta table for a key
/value
pair other than a full blown post search with WP_Query
. I’d consider that overkill. Just query the table:
$unique = $wpdb->get_var(
"SELECT meta_id
FROM {$wpdb->postmeta}
WHERE meta_key = 'unique_code'
AND meta_value="{$random}"
LIMIT 1"
);
I can’t help but think that more detail in the question might point to a better way to proceed
Related Posts:
- Meta Query with AND & OR?
- Upcoming Event: How do I sort database by custom date field, but ignore past dates?
- Conditional custom field query
- Query post by date (stored custom field meta as yyyymmdd) and differentiate across 12 months
- How to query for posts with either one or another custom field
- Complex WP_Query Using Post Date And Post Meta
- WP_Query – Order results by meta value
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- Filter WP_Query for posts having a certain meta-value
- Can I query custom meta data through WP_Query
- How to rename a custom field?
- How do I search an array stored in a custom-field using WP_Query?
- Order Posts by Closest Numeric Values
- 2 orderby in wp_query with 2 custom fields
- Can serialized arrays in DB be matched against serialized arrays with meta_query?
- How to get a meta value from all post
- Compare meta_query decimals not working right
- Query Problem – Show posts within category ‘x’ that have a custom field between ‘y’ and ‘z’
- Comparing timestamps in meta query doesn’t work
- Display two post types ordered by two custom fields
- Using OR in WP_Query negates the “NOT EXISTS” compare
- WP_query : meta_key with custom rule for specific value
- How can I combine meta_query queries?
- How to filter a dd/mm/yyyy date from a custom field in a query
- Having trouble with custom date field for CPT query (WordPress)
- Using WP Query to search by multiple meta fields
- Sort by posts that have a featured image?
- WP_Query display next custom post from today’s date
- Order by value in serialized custom field
- $wp_query meta_key naming issue [closed]
- Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
- How do I query for a post by custom field?
- Meta_query and numeric comparison [closed]
- Conditional to modify query results
- Adding Custom Fields to Search
- sort search results by custom fields using dropdown
- Multiple relationships in a query
- How can I sort homepage by a meta value?
- Loop to display random posts only if a custom field matches category
- WP Query Returning All Posts
- How do I use wp_query for WordPress search?
- Meta query with timestamp using WP_query
- Order query by meta_value with multiple custom fields
- Display only past events on that page using Visual Composer Grid Bulider
- Add quicklink to in the Admin posts page where I can query by a meta_key
- Custom Field Create Bulk via SQL Query
- Create Pop-Up Box with Custom Field Content Inside the Loop
- query usermeta from custom field
- Order RSS content by an advanced custom field value
- Order by empty custom field
- Alter main archive, to show posts with meta as last
- First custom field value (out of several) displayed twice after query
- Custom WP_Query for WordPress Search Results with meta_query
- Meta Query And/Or
- How to combine custom fields to make one order-able value
- Trying to reorder posts by custom field
- Why orderbyb meta_value_num won’t affect the generated SQL Query order?
- Get post ids sorted by meta_key
- querying a custom field
- Passing meta_box string to post__in?
- pre_get_posts : only get posts by wp_usermeta value
- meta_value and meta_key filtering returning no posts [closed]
- Can’t query by meta_key
- How to query an out put value?
- Modify date query by URL parameter using pre_get_posts and $_GET
- Loop through incrementing custom fields
- Custom search SQL Query to add custom field in result
- Meta query relation ‘OR’ not working as expected
- How to exclude custom fields from search queries?
- Custom Query Fields – Altering Meta Value
- Multiple Custom Field Query
- Custom Query: query by post custom meta data
- Get array of posts based on custom field values
- How to update custom field of a posts in a particular category
- How to query posts by month based on date custom field?
- How can I modify my meta_query to work with prices that are stored in the database that contain dollar signs and commas?
- Custom Field Date Problem
- Query multiple post types, but different order for each
- Problem with revisions only returning four results
- Search & column order by meta value in admin
- WP Query from two Custom Post type fields as statement
- Dynamically populate query source in Elementor post widget
- WP_Query not ordering correctly
- Query against multiple locations within single custom post type post
- searching by keywords in post’s metas or pagination links problem
- Query by meta value (add a dropdown of all values)
- select user with all meta field and field value. I am use Below Query for this ,So any of know another way to fast query instead of below query?
- Create custom query for search?
- Loop through multiple custom fields with increasing number
- WP Query multiple select form – meta_query help
- How to Order Posts by Custom Fields?
- WP_Query based on multiple metadata comparisons
- Get multiple posts with some custom fields efficient
- Display posts where date field matches current month?
- Show posts containing or not custom field
- How to check if custom field exists in this widget query
- Only show content slideshow if “slideshow” custom field exists
- Display agents (custom post type) alphabetically, except one who always shows last
- Query posts by Custom Meta (checkbox) & Genesis Grid Loop
- How to display search query as formatted text?