Custom post type not showing posts inserted from frontend

Hey everyone,

I have a post type called `index`, and i’m trying to build a form for users to insert posts from the frontend.

Read More

i created a page and a template for it with a form and an ajax function which insert the content to the DB using `wp_insert_post`. the function works generally, i can see the new posts added to the wp_post db in phpmyadmin. the problem is that i can’t see the new posts in the admin panel. the post are counted (i can see the number goes up everytime i try the form), but not shown.

this is the functions.php ajax code (some $_get vars are to be used for meta data inserting):

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;  
}

and this is the index post type code:

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

Related posts

Leave a Reply

2 comments

  1. okay i found it!
    the problem was i already had a pre_get_posts hook that changes the order of the index archive according to the 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' );
    
        }
    }
    

    i added && !is_admin() to the if and it turned okay.
    thank you Arif, your answer helped me find it.

  2. you can add an action hook ‘pre_get_posts’ to include/exclude your custom post type to/from the admin post list or any for any other listing like archives.

    add_filter( ‘pre_get_posts’, ‘include_custom_type_index’ );

    function include_custom_type_index( $query ) {

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

    }