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

标签 php mysql ajax wordpress

大家好

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

我使用表单和 ajax 函数创建了一个页面和一个模板,该函数使用“wp_insert_post”将内容插入到数据库中。该功能通常有效,我可以在 phpmyadmin 中看到添加到 wp_post 数据库的新帖子。问题是我在管理面板中看不到新帖子。帖子被计算在内(我每次尝试该表格时都可以看到数字增加),但未显示。

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

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');

最佳答案

好的,我找到了! 问题是我已经有了一个 pre_get_posts Hook ,它根据帖子元更改索引存档的顺序。

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' );

    }
}

我在 if 中添加了 && !is_admin() 并且一切正常。 谢谢 Arif,你的回答帮助我找到了它。

关于php - 自定义帖子类型不显示从前端插入的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26075066/

相关文章:

php - 使用 vim Tabularize 插件只匹配第一次出现的定界符

php - 按日期获取不同表中记录之间的持续时间

mysql - 无论如何,我可以使用单个 MySQL 查询在多个表中插入记录吗?

java - 如何从没有提交按钮的情况下调用 Struts2 操作

php - 找不到我的 SQL 语法 - PHP 中的错误(检查语法手册?)

php - 无法在一行帖子中添加 margin-right

php - MySQL 和 PHP 中多个数据库的最佳性能解决方案

php - 如何从其他计算机访问我的本地主机服务器?

mySQL - sql查询多连接问题

jquery - ajax 无法在 Codeigniter 中工作