Last active
August 29, 2015 14:27
-
-
Save azizultex/180b3d1c98d2dfea6424 to your computer and use it in GitHub Desktop.
Show Only Selected Custom Posts from Meta Box in a Page using Piklist
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
// meta box that creates checkbox list with a list of 'objects' custom post type post titles | |
$args = array('post_type' => 'objects', 'posts_per_page' => -1); | |
$objects = get_posts( $args ); | |
$post_ids = wp_list_pluck( $objects , 'ID' ); | |
$object_titles = wp_list_pluck( $objects , 'post_title' ); | |
$combined_array = array_combine($post_ids, $object_titles); // create an associative array where ids are key and titles are value | |
piklist('field', array( | |
'type' => 'checkbox' | |
,'field' => 'object_list' | |
,'label' => __('Object List', 'bbhs') | |
,'description' => 'The object lists that will be visible under sub-categories only' | |
,'attributes' => array( | |
'class' => 'object_list_checkbox' | |
) | |
,'choices' => $combined_array | |
)); | |
// using code in query | |
$selectedids = get_post_meta( $post->ID, 'object_list' ); | |
if($selectedids) { | |
$args = array( | |
'post_type' => 'objects', | |
'post__in' => $selectedids | |
); | |
} else { | |
echo '<div style="display:block;overflow:hidden; margin: 50px auto;"><h4>No Objects Selected to show</h4></div>'; | |
} | |
$objects = new WP_Query( $args ); | |
if($objects->have_posts()) { | |
echo '<div class="objects_lists">'; | |
// The Loop | |
while ( $objects->have_posts() ) { | |
...... | |
// pretty easy, huh? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suppose, I have two custom post type 'sections' & 'objects'. Now that I want to show selected 'objects' at the bottom of every 'section' page (single-sections.php) and the selection will be done when create a new 'section' from a checkbox in 'select' post type. The above code does this easily.
Screenshots
Output
Here are some similar gists by me:
https://gist.github.com/azizultex/614d294e3893e342538b
https://gist.github.com/azizultex/f0b1aae10279c9a7233d