Skip Navigation

[Resolved] How can I add a custom post to my WP site (cont')

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 5 replies, has 3 voices.

Last updated by Minesh 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1740233

Dear Sir/Madam,

Refer to https://toolset.com/forums/topic/how-can-i-add-a-custom-post-to-my-wp-site/ I tried to run below script

  define('WP_USE_THEMES', false);
  define( 'WP_DEBUG', true );
  
  require_once('wp-load.php');
	
	$my_post = array(
		
	  'post_title'    => 'CR Gas Raises Stake In Nanjing Jiangning To 70 To Enhance Overall Business Performance',
	  'post_type'     => 'press-release',
	  'post_status'   => 'publish',
	  'post_author'   => 'cplink',
	  'meta_input' => array(
	    'wpcf-press_date' => '2011-11-09 00:00:00',
	    'wpcf-press_pdf' => 'CRGas_Jiangning JV Stake',
	    'wpcf-press-organisation' => ''
	  )
	);
	  
	// Insert the post into the database
	wp_insert_post( $my_post, 1 );

but can't complete, no error prompt, how can I troubleshoot the problem?

Best regards,

Kelvin.

#1740371

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Kelvin and thank you for contacting the Toolset support.

Can you provide more details about this code?
- Can you provide the whole code? Is this code inside a function?
- Can you tell me where(what file) this code is added?

It seems that your code tries to load WordPress with this line:

require_once('wp-load.php');

This use is not supported by WordPress, so we, Toolset, do not support it either.

The custom code should be wrapped in a function and executed on a WordPress hook (filter/action), that's the supported way. Check this article https://toolset.com/documentation/programmer-reference/toolset-hooks/

#1740821

Dear Jamal,

require_once('wp-load.php');

above is an official script file for calling the WP. Below is the script that I can query the post from WP

    define('WP_USE_THEMES', false);
    define( 'WP_DEBUG', true );
  
    require_once('wp-load.php');
	
    $args = array(
    'post_type' => 'press_release',
    'posts_per_page' => '-1',
	);
	
	$query = new WP_Query( $args );

// The Loop
	$count = 0;
	while ( $query->have_posts() ) {
	    $query->the_post();
	    $count++;
	    echo "<li>{$count} " . get_the_title() . '</li>';
	}			

You can find the information about this script from https://wordpress.stackexchange.com/questions/47049/what-is-the-correct-way-to-use-wordpress-functions-outside-wordpress-files/47059

The code in my previous post is the full code. There is no error but also no data insert at last.

Best regards,

Kelvin.

#1743125

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Jamal is on Vacation. This is Minesh here and I'll take care of this ticket. Hope this is OK.

Looking at your requirement, the question you raised is a pure WordPress question and nothing related to Toolset. I suggest you should raise this question in public forum like: https://wordpress.stackexchange.com

As per our support policy, we do not support such custom code, its beyond the scope of our support policy. If you need custom programming help for your site, you can always contact our certified partners:
=> https://toolset.com/contractors/

#1743763

Dear Minesh,

No, my question is related to Toolset how I can import Toolset custom post into WP DB. WP doesn't advise 3rd party plugins.

Best regards,

Kelvin.

#1745079

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To import custom post you can go for CSV file and we support few plugins using which you can import the CSV data.

Please check the following Docs:
=> https://toolset.com/course-lesson/how-to-import-content-into-wordpress-using-csv/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.