【问题标题】:Custom post type not showing posts inserted from frontend自定义帖子类型不显示从前端插入的帖子
【发布时间】:2014-09-27 13:18:01
【问题描述】:

大家好,

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

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

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

【问题讨论】:

    标签: php mysql ajax wordpress


    【解决方案1】:

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

    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,你的回答帮我找到了。

    【讨论】:

      【解决方案2】:

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

      add_filter('pre_get_posts', 'include_custom_type_index');

      函数 include_custom_type_index( $query ) {

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

      }

      【讨论】:

      • 谢谢,我试试看。
      猜你喜欢
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2021-12-25
      • 2016-12-29
      • 1970-01-01
      相关资源
      最近更新 更多