我正在开发一个主题,出现以下错误“Warning: trim() expects parameter 1 to be string, array given in C:\\wamp\\www\\themes\\wp-includes\\query.php on line 1609
“我已经尝试了我能想到的一切,并且在谷歌上搜索的时间比我想承认的要长得多。
The Issue
我创建了一个表单,允许用户提交配方并使用
wp_insert_post
在网站的前端。问题是我设置的重复字段。它们在仪表板上工作正常,但显然需要进行修改。他们提交到一个数组,并正确提交到我的帖子类型,但不断抛出上面的错误。
如果有人能看一下,让我知道我能做些什么来清理它,我将不胜感激。
Not sure if it matters but here is how I am duplicating the fields
<script type="text/javascript">
jQuery(document).ready(function( $ ){
$( \'#add-row\' ).on(\'click\', function() {
var row = $( \'.empty-row.screen-reader-text\' ).clone(true);
row.removeClass( \'empty-row screen-reader-text\' );
row.insertBefore( \'#repeatable-ingredient-set tbody>tr:last\' );
return false;
});
$( \'.remove-button\' ).on(\'click\', function() {
$(this).parents(\'tr\').remove();
return false;
});
});
</script>
Here is the section that\'s causing the issue
<!-- Add the repeating ingredient fields -->
<tr>
<td><input type="text" class="widefat" name="name[]" /></td>
<td><input type="text" class="widefat" name="amount[]" /></td>
<td>
<select name="unit[]">
<?php foreach ( $options as $label => $value ) : ?>
<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
</td>
<td><a class="button remove-button" href="#">Remove</a></td>
</tr>
<!-- Hidden field group used when add button is pressed (jQuery) -->
<tr class="empty-row screen-reader-text">
<td><input type="text" class="widefat" name="name[]" /></td>
<td><input type="text" class="widefat" name="amount[]" /></td>
<td>
<select name="unit[]">
<?php foreach ( $options as $label => $value ) : ?>
<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
</td>
<td><a class="button remove-button" href="#">Remove</a></td>
</tr>
Here is where it gets submitted
//* Create a blank array for the newly added ingredients
$ingredients = array();
//* Get the names
$names = $_POST[\'name\'];
//* Get the measurements
$amounts = $_POST[\'amount\'];
//* Get the units ($options)
$units = $_POST[\'unit\'];
//* Find the number of ingredients by counting the names
$count = count( $names );
//* As long as there are ingredients then add them to the array
for ( $i = 0; $i < $count; $i++ ) {
if ( $names[$i] != \'\' ) :
$ingredients[$i][\'name\'] = stripslashes( strip_tags( $names[$i] ) );
$ingredients[$i][\'amount\'] = stripslashes( strip_tags( $amounts[$i] ) );
if ( in_array( $units[$i], $options ) )
$ingredients[$i][\'unit\'] = $units[$i];
else
$ingredients[$i][\'unit\'] = \'\';
endif;
}
//* If all requirements are met then proceed to build the new recipe
if ( isset($_POST[\'recipe_title\']) && isset($_POST[\'recipe_content\']) && ( isset($hasError) ==false )) {
global $wpdb;
//* Add all the information collected from the form
$new_post = array(
\'post_title\' => $recipe_title, //* Add the title
\'post_content\' => $recipe_content, //* Add the instructions
\'meta_input\' => array(
\'cook_time_hour\' => $cook_time_hour, //* Add the cook time hours
\'cook_time_minute\' => $cook_time_minute, //* Add the cook time minutes
\'servings\' => $servings, //* Add the servings
\'calories\' => $calories, //* Add the calories
\'measurement_units\' => $ingredients, //* Add the ingredients
),
\'tax_input\' => array(
\'recipe_types\' => array( $recipe_cat[0] ), //* Add the recipe type
),
\'post_status\' => \'pending\', //* Set the new recipe as pending to be reviewed
\'post_type\' => \'recipes\' //* Set it to the recipes post type
);
//* Get the post ID of the newly created recipe
$post_id = wp_insert_post($new_post);
我知道名字的属性,
name[]
,
amount[]
, 和
unit[]
, 是问题所在,但不确定是否有办法解决此问题,以便在不进行大量修改的情况下使用WordPress。
提前感谢!