我正在创建一个客户门户,我的客户可以维护他们的项目并在Wordpress中留下反馈。我有两种自定义的帖子类型,分别称为“客户端”和“项目”,它们都在后端从另一个站点获取信息。当我创建客户机时,它会自动将其postID生成到项目post类型上的下拉菜单中,我可以将客户机分配给项目。
我试图完成的是在前端的一个页面上显示与所选客户机关联的所有项目。单个页面将是客户端门户,由客户端帖子类型生成。
我似乎无法显示关联的帖子。这是我的代码single.php
将在客户端门户上显示项目。
<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$loop = new WP_Query( array(
\'post_type\' => array( \'projects\'),
\'posts_per_page\' => -1,
\'paged\' => $paged,
\'meta_query\' => array(
array(
\'key\' => \'a_client\', // name of custom field
\'value\' => \'"\' . get_the_ID() . \'"\',
\'compare\' => \'LIKE\'
)
)
));
?>
下面是我用来将客户机分配给项目管理面板中的项目的代码。该脚本显示我在客户端页面中创建的任何客户端,并在下拉菜单中显示它们的名称。
add_action( \'add_meta_boxes\', \'add_clients_custom_metabox\' );
function add_clients_custom_metabox() {
add_meta_box( \'custom-metabox\', __( \'Clients\' ), \'clients_custom_metabox\', \'projects\', \'side\', \'high\' );
}
function clients_custom_metabox($post) {
global $post,$current_user;
//remember the current $post object
$real_post = $post;
//get curent user info (we need the ID)
get_currentuserinfo();
//create nonce
echo \'<input type="hidden" name="clients_meta_box_nonce" value="\', wp_create_nonce(basename(__FILE__)), \'" />\';
//get saved meta
$selected = get_post_meta( $post->ID, \'a_clients\', true );
//create a query for all of the user clients posts
$clients_query = new WP_Query();
$clients_query->query(array(
\'post_type\' => \'client_portal\',
\'posts_per_page\' => -1,
\'author\' => $current_user->ID));
if ($clients_query->have_posts()){
echo \'<select name="a_clients" id="a_clients">\';
//loop over all post and add them to the select dropdown
echo \'<option>Assign a client</option>\';
while ($clients_query->have_posts()){
$clients_query->the_post();
echo \'<option value="\'.$post->ID.\'" \';
if ( $post->ID == $selected){
echo \'selected="selected"\';
}
echo \'>\'.$post->post_title .\'</option>\';
}
echo \'<select>\';
}
//reset the query and the $post to its real value
wp_reset_query();
$post = $real_post;
}
//hook to save the post meta
add_action( \'save_post\', \'save_clients_custom_metabox\' );
// Process the custom metabox fields
function save_clients_custom_metabox( $post_id ) {
global $post;
// verify nonce
if (!wp_verify_nonce($_POST[\'clients_meta_box_nonce\'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if (\'events\' == $_POST[\'post_type\']) {
if (!current_user_can(\'edit_page\', $post_id)) {
return $post_id;
}
} elseif (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
if( $_POST ) {
$old = get_post_meta($post_id, \'a_clients\', true);
$new = $_POST[\'a_clients\'];
if ($new && $new != $old){
update_post_meta($post_id, \'a_clients\', $new);
}
}
}
单件的完整代码。php
http://pastebin.com/na7djwsq我正在使用的注册帖子类型Project Post Type调用=>projects调用客户端帖子类型=>client_portal