i have a custome post type known as "Motor_supply"
i want to show "Motor_supply" titles by a category as a "selectbox" in metabox on shop_order
i want to know how can i create a loop and put published "Motor_supply" posts titles in it
i have this code
class select_motor {
private $config = '{"title":"\u062a\u062e\u0635\u06cc\u0635 \u0645\u0648\u062a\u0648\u0631","prefix":"select_motor","domain":"select_motor","class_name":"select_motor","post-type":["post"],"context":"normal","priority":"default","cpt":"shop_order","fields":[{"type":"select","label":"select motor","options":"option-one : MOTOR_1\r\noption-one : MOTOR_2","id":"select_motorselect-motor"}]}';
public function __construct() {
$this->config = json_decode( $this->config, true );
$this->process_cpts();
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
add_action( 'save_post', [ $this, 'save_post' ] );
}
public function process_cpts() {
if ( !empty( $this->config['cpt'] ) ) {
if ( empty( $this->config['post-type'] ) ) {
$this->config['post-type'] = [];
}
$parts = explode( ',', $this->config['cpt'] );
$parts = array_map( 'trim', $parts );
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts );
}
}
private function select( $field ) {
printf(
'<select id="%s" name="%s">%s</select>',
$field['id'], $field['id'],
$this->select_options( $field )
);
}
private function select_options( $field ) {
$output = [];
$options = explode( "\r\n", $field['options'] );
$i = 0;
foreach ( $options as $option ) {
$pair = explode( ':', $option );
$pair = array_map( 'trim', $pair );
$output[] = sprintf(
'<option %s value="%s"> %s</option>',
$this->select_selected( $field, $pair[0] ),
$pair[0], $pair[1]
);
$i++;
}
return implode( '<br>', $output );
}
private function value( $field ) {
global $post;
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) {
$value = get_post_meta( $post->ID, $field['id'], true );
} else if ( isset( $field['default'] ) ) {
$value = $field['default'];
} else {
return '';
}
return str_replace( '\u0027', "'", $value );
}
}
new select_motor;