To query posts by custom fields you can use the ‘meta_query’ parameter
<?php
$args = array(
'post_type' => 'payment',
'meta_query' => array(
array(
'key' => 'bookingref',
'value' => 'the_value_you_want',
'compare' => 'LIKE'
),
array(
'key' => 'customerref',
'value' => 'the_value_you_want',
'compare' => 'LIKE'
)
);
query_posts($args); while (have_posts()) : the_post(); ?>
you can’t use get_post_meta inside the query because it gets you the value and not the key and also it accepts a Post ID to get that value of and before the query $post->id is not in the scope.
Related Posts:
- Custom query with orderby meta_value of custom field
- Using get_posts with arguments found in meta keys
- query_posts and only show results if a custom field is not empty
- Filter archive.php by custom meta
- Order posts by custom field and if custom field is empty return remaining posts
- Getting movie and serial on actor page
- Loop through two different sets of custom fields
- Query on custom field count?
- use get_posts to get custom field data, but in one array
- How to use query_posts() with a date filter on a custom field?
- Order posts according to user defined order for meta values?
- meta_value_num sort glitch
- Can ordering post list by meta_value cause performance issue?
- Order by value in serialized custom field
- 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 not showing any results?
- how can i use custom field in query post
- using multiple meta_key and meta_value in query_posts
- Query post order by post and desc not working
- How can I sort homepage by a meta value?
- Order query by meta_value with multiple custom fields
- get_posts that match a user-specified value on a page
- Show 1 post and after a specific date show the next one
- If meta key exists in get posts function otherwise create it
- get_posts that haven’t been assigned a specific custom field
- How to select posts from multiple categories and metavalues?
- meta_compare seems to be treating values as strings instead of integers as expected
- Meta Query returns wrong number of posts
- How to find a post using XML-RPC without knowing ID
- Change order of posts
- get_posts in meta box dropdown not showing latest posts
- filter custom field values $min $max
- get post id using custom filed value
- query posts custom field calculation value
- WP Query – Is this correct?
- Query_posts with custom field meta value
- Unable to get specific value from post meta
- Posts query according to meta box date
- Meta_query ‘compare’ => ‘LIKE’ not working?
- Loop through incrementing custom fields
- query_posts with meta_value
- Custom search SQL Query to add custom field in result
- How to put forward a blog post
- How can I change the publish date based on a custom field?
- Custom field to array?
- Get author total post votes from post meta
- Problem with writting correctly a query posts args in WordPress
- simple fields plugin custom query
- How do I display an article using a WordPress custom field?
- query_posts -> get page_id from custom field
- Checking if field is set before comparing with meta_query in query_posts?
- get_post_custom_values not working
- Filtering posts by WORD in custom field
- Display agents (custom post type) alphabetically, except one who always shows last
- Filter Query Post by Custom Fields(by date)
- get_the_ID() retrieves same ID on Gutenberg’s Query Loop
- When should you use WP_Query vs query_posts() vs get_posts()?
- When to use WP_query(), query_posts() and pre_get_posts
- Advanced Custom Fields: how do I check to see if a value is set in an field? [closed]
- WP_Query – Order results by meta value
- Add filter menu to admin list of posts (of custom type) to filter posts by custom field values
- getting all values for a custom field key (cross-post)
- How to add a custom field in the advanced menu properties?
- How to filter post listing (in WP dashboard posts listing) using a custom field (search functionality)?
- what is the correct way to compare dates in a WP query_posts meta_query
- Using meta query (‘meta_query’) with a search query (‘s’)
- Can I exclude a post by meta key using pre_get_posts function?
- Add validation and error handling when saving custom fields?
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- Show Custom Fields in Quick Edit
- Where are custom field values stored in the database
- Validating Custom Meta Box Values & Required Fields
- Max length of meta_value
- Add custom fields to wp native gallery settings
- How to fix missing custom fields after upgrading to WordPress 4.8.1?
- How to enable custom fields for pages (if not a bad practice)?
- How can I add extra attribute in the ‘Page Attribute’ section in wp-admin for pages?
- Is there a way to set default custom fields when creating a post?
- Custom post meta field effect on the performance on the post
- How to get post content by calling ajax?
- How to get custom post meta using REST API
- Custom field/meta populated by dropdown of existing posts?
- Difference between meta keys with _ and without _ [duplicate]
- Is there any action filter/hook for validating a custom field before publishing the post?
- Get the ID of the latest post
- Remove Dimension from wp_get_attachment_image
- Orderby meta_value only returns posts that have existing meta_key
- Underscores in custom fields
- What is the index [0] for on post meta fields?
- Using meta_query, how can i filter by a custom field and order by another one?
- What is “meta_input” parameter in wp_insert_post() used for?
- How to show related posts by category
- How to enable revisions for post meta data?
- Sortable Custom Columns in User Panel (users.php)?
- Any way to add custom options to Gallery Settings?
- Return all custom meta data for one custom post type
- WP_Query vs get_posts
- The “_encloseme” Meta-Key Conundrum
- ORDER BY custom field value