• Hi,

    I’m trying to figure out what I’m doing wrong in the attached coding example.

    I can successfully post to wordpress, but I can’t figure out how to change the post_format value to “link” successfully. No matter what I have tried, the post_format value is set to ‘standard’. I need it set to ‘link’ and also be able to designate a URL. I’ve spent lots of time looking at posts on the internet with no luck. I’m missing ‘something’.

    The php file for testing resides in the root directory of my wordpress installation.

    <?php
    //figure out how to add a post with link format and URL
    
    error_reporting(-1);
    $location = $_SERVER['DOCUMENT_ROOT'];
    
    echo $location;
    //exit;
    include ($location . '/wp-config.php');
    include ($location . '/wp-load.php');
    include ($location . '/wp-includes/pluggable.php');
    //include_once($location.'/wp-includes/post-formats.php');
    
    require($location . '/wp-blog-header.php');
    // Create post object
    $my_post = array();
    $my_post['post_title'] = 'Title of this Post';
    $my_post['post_content'] = 'Some Content to Add';
    $my_post['post_status'] = 'publish';
    $my_post['post_type']= 'post';
    //post_category is correct here
    $my_post['post_category'] = array(34);
    //post_format always goes to 'standard'
    //tried array(2) also with no luck
    $my_post['post_format']='link';
    
    //very array
    print_r ($my_post);
    
    //all the below commented options were tried with no success.
    //set_post_format('', 'link' ); //sets the given post to the 'link' format
    //set_post_format($mypost->'ID', 'link' ); //sets the given post to the 'link' format
    //$my_post['post_link_url'] = "https://www.SomeTestUrl.com";
    //$my_post['post_link'] = "https://www.SomeTestUrl.net";
    
    // Insert the post into the database
    if( null == get_page_by_title( $my_post['post_title'] ) ) {
    wp_insert_post( $my_post );
    }
    
    //Post is successfully added but can't change post_format value to 'link'
    
    ?>
Viewing 1 replies (of 1 total)
  • It looks liek you are using the $post object incorrectly:
    $mypost->'ID' is invalid.

    First off, you’d have to set the post format AFTER the post is created. It looks like you are doing it before wp_insert_post. I’d try the following to see if it works:

    //include_once($location.'/wp-includes/post-formats.php');
    
    require($location . '/wp-blog-header.php');
    // Create post object
    $my_post = array();
    $my_post['post_title'] = 'Title of this Post';
    $my_post['post_content'] = 'Some Content to Add';
    $my_post['post_status'] = 'publish';
    $my_post['post_type']= 'post';
    $my_post['post_category'] = array(34);
    
    // Insert the post into the database
    if( null == get_page_by_title( $my_post['post_title'] ) ) {
    	// wp_insert_post() returns the craeted post ID
    	$post_id = wp_insert_post( $my_post );
    
    	if ( ! empty( $post_id ) ) {
    		// Set the post format
    		set_post_format( $post_id, 'link' );
    
    		// Use post meta for the link you want to reference?
    		update_post_meta( $post_id, 'post_link_url', $url );
    	}
    }

    I haven’t fully tested this, mainly for one reason, this is a REALLY insecure way to insert a post. You are directly hooking into functions that are meant to be used by authenticated users only, however you are bypassing all the authentication.

    If you are wanting to create a custom submission of a new post of a format, you might want to look at writing an extension to one of the popular form plugins.

Viewing 1 replies (of 1 total)
  • The topic ‘Coding : How to change post_format value’ is closed to new replies.