Last active
August 24, 2019 19:59
-
-
Save craigedmonds/e7d3d526391a755ab38f922d0c9326fb to your computer and use it in GitHub Desktop.
WP_Query - search CPT and two custom fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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