Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP_Query - search CPT and two custom fields
<?php
########################
// Here you can search a post type or post and find posts
// with two different custom field data (keys).
// This is useful if you have setup two custom fields and storing
// data in the fields and need to find posts that match.
// In the case below I have two custom fields: content_client_id and content_status
// content_client_id = the id of a client
// content_status = the status of a post (EG: Awaiting Approval by Client)
// I want to find all posts that match a client id and Awaiting Approval by Client
// and then order the posts by another custom field called: content_scheduled_posting_date
########################
$args = array(
'post_status' => 'publish',
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'content_client_id',
'value' => $client_id,
'compare' => '=',
),
array(
'key' => 'content_status',
'value' => 'Awaiting Approval by Client',
'compare' => '=',
),
),
'orderby' => 'content_scheduled_posting_date',
'order' => 'ASC',
);
$results = new WP_Query( $args );
//convert object to assocoiative array as I dont like working with WP Objects very much
$results = json_decode(json_encode($results), true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.