自定义帖子类型不显示从前端插入的帖子

时间:2014-09-27 13:18:01

标签: php mysql ajax wordpress

嘿大家,

我有一个名为`index`的帖子类型,我正在尝试构建一个表单,供用户从前端插入帖子。

我使用表单和ajax函数为它创建了一个页面和一个模板,该函数使用`wp_insert_post`将内容插入到数据库中。该函数通常工作,我可以看到在phpmyadmin中添加到wp_post数据库的新帖子。问题是我无法在管理面板中看到新帖子。这个帖子被计算在内(我可以看到每次尝试表格时数字都会上升),但是没有显示。

这是functions.php ajax代码(一些$ _get vars用于元数据插入):

add_action('wp_ajax_addtoindex', 'addtoindex');
add_action('wp_ajax_nopriv_addtoindex', 'addtoindex');

function addtoindex(){
$title = $_GET["title"];
$slug = sanitize_title_with_dashes($title,'','save');
$group = $_GET["group"];
$inst = $_GET["inst"];
$location = $_GET["location"];
$address = $_GET["address"];
$content = $_GET["content"];
$website = $_GET["website"];
$year = $_GET["year"];
$educ = $_GET["educ"];
$aud = $_GET["aud"];
$teaching = $_GET["teaching"];
$teachers = $_GET["teachers"];
$contact1 = $_GET["contact1"];
$email1 = $_GET["email1"];
$phone1 = $_GET["phone1"];
$contact2 = $_GET["contact2"];
$email2 = $_GET["email2"];
$phone2 = $_GET["phone2"];
$user = get_user_by("login",$authorid);
$authorid = $user->ID;

// Check if the group exists
$group_term = term_exists( $group, 'group', 0 );

// Create group if it doesn't exist
if ( !$group_term ) {
    $group_term = wp_insert_term( $group, 'group', array( 'parent' => 0 ) );
}

// Check if the inst exists
$inst_term = term_exists( $inst, 'inst', 0 );

// Create inst if it doesn't exist
if ( !$inst_term ) {
    $inst_term = wp_insert_term( $inst, 'inst', array( 'parent' => 0 ) );
}

// Check if the location exists
$location_term = term_exists( $location, 'location', 0 );

// Create location if it doesn't exist
if ( !$location_term ) {
    $location_term = wp_insert_term( $location, 'location', array( 'parent' => 0 ) );
}

$custom_tax = array(
    'group' => $group_term,
    'inst' => $group_inst,
    'location' => $group_location
);

//Post Properties
$new_post = array(
    'post_title'    => $title,
    'post_name' => $slug,
    'post_content'  => $content,
    'post_status'   => 'pending',
    'post_type' => 'index',
    'post_author' => $authorid,
    'comment_status' => 'closed',
    'ping_status' => 'closed',
    'tax_input'    => $custom_tax
);

//save the new post
if ( post_type_exists( 'index' ) ) {
$pid = wp_insert_post($new_post, true);
   echo 'good';
}
else{
   echo "bad";
}

// Reset Post Data  
wp_reset_postdata();  

exit;  
}

这是index帖子类型代码:

function post_type_index() {
register_post_type( 'index',
            array( 
            'label' => __('Index'), 
            'labels' =>  array('name' => 'אינדקס האנתרופוסופיה','singular_name' => __('פריט לאינדקס','ohav'),'edit_item' => __('עריכת פריט אינדקס','ohav'),'add_new' => __('הוספת פריט לאינדקס','ohav'),'add_new_item' => __('הוספת פריט לאינדקס','ohav'),'all_items' => __('לכל פריטי האינדקס','ohav')), 
            'public' => true,
            //'publicly_queryable' => true,
            //'query_var'   => true,
            //'capability_type'    => 'post',
            'has_archive' => true,
            'show_ui' => true,
            'show_in_nav_menus' => true,
            'rewrite' =>array(
                'slug' =>  __('index','ohav'),
                'with_front' => true
            ),
            'hierarchical' => true,
            'supports' => array(
                    'title',
                    'boxplace',
                    'editor',
                    'thumbnail'
                    )
                ) 
            );


}
add_action('init', 'post_type_index');

2 个答案:

答案 0 :(得分:1)

好吧,我发现了! 问题是我已经有一个pre_get_posts钩子,它根据post meta改变了索引档案的顺序。

add_action( 'pre_get_posts', 'change_order_for_index' );

function change_order_for_index( $query ) {
    if ( is_post_type_archive('index') ) {
        $query->set( 'meta_key', '_index_featured' );
        $query->set( 'orderby', 'meta_value' );

    }
}

我将&& !is_admin()添加到if并且转好了。 谢谢Arif,你的回答帮助我找到了它。

答案 1 :(得分:0)

您可以添加一个操作挂钩'pre_get_posts',以便在管理员帖子列表中包含/排除您的自定义帖子类型,也可以添加任何其他列表(例如档案)。

add_filter('pre_get_posts','include_custom_type_index');

function include_custom_type_index($ query){

$query->set( 'post_type', array(
 'post', 'index'
    ));
return $query;

}