admin管理员组文章数量:1402181
I have been building a custom word press plugin that takes in a json file and updates a list of posts with meta data. But I cant figure out how to pull the meta data. I have created some logs that show that the meta data is in fact being stored properly. I just can't figure out why it is not pulling.
Display, shows title, content, data but Post Meta is empty:
export default function HousingListingsBlockEdit( { attributes } ) {
const blockProps = useBlockProps();
// Retrieve housing_listing posts from the REST API.
const listings = useSelect( ( select ) => {
return select( 'core' ).getEntityRecords( 'postType', 'housing_listing', {
per_page: 1,
context: 'edit'
} ) || [];
}, [] );
return (
<div { ...blockProps }>
<div id="hl-results" style={ { maxWidth: '1000px', margin: '0 auto' } }>
{ finalListings.length ? (
finalListings.map( ( post ) => {
return (
<div { ...blockProps }>
{ post.id ? (
<div>
<p><strong>Post Name:</strong> { post.title.rendered }</p>
<p><strong>Post Date:</strong> { post.date }</p>
<p><strong>Post Description:</strong></p>
<div dangerouslySetInnerHTML={ { __html: post.content.rendered } } />
<p><strong>Post Meta:</strong></p>
<pre>{ JSON.stringify( post.meta, null, 2 ) }</pre>
</div>
) : (
<p>{ __( 'No listing found.', 'hrdc-custom-tools' ) }</p>
) }
</div>
);
})
) : (
<p>{ __( 'No listings found.', 'n-custom-tools' ) }</p>
) }
</div>
</div>
);
}
And in my render page I am getting the post meta as seen here:
$query = new WP_Query( array(
'post_type' => 'housing_listing',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
'cache_results' => false,
) );
$container_class = isset( $attributes['containerClass'] ) ? $attributes['containerClass'] : '';
$container_style = isset( $attributes['containerStyle'] ) ? $attributes['containerStyle'] : '';
$output = '<div class="n-housing-listings ' . esc_attr( $container_class ) . '" style="' . esc_attr( $container_style ) . '">';
$output .= '<div id="hl-results">';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
// Retrieve meta values once.
$address = get_post_meta( $post_id, '_address', true );
$city = get_post_meta( $post_id, '_city', true );
$manager = get_post_meta( $post_id, '_property_manager', true );
$phone = get_post_meta( $post_id, '_phone', true );
$website = get_post_meta( $post_id, '_website', true );
$category = get_post_meta( $post_id, '_category', true );
Further more I have localized the data in my main plugin file:
Function n_register_meta() {
$meta_fields = [
'_address' => 'string',
'_city' => 'string',
'_county' => 'string',
'_property_manager' => 'string',
'_phone' => 'string',
'_website' => 'string',
'_category' => 'string',
'_reserved_for' => 'string', // For filtering
'_application_fee' => 'string', // For filtering
'_felonies_considered' => 'string', // For filtering
'_credit_check_not_required' => 'string', // For filtering
'_unit_types' => 'string', // For filtering
'_pets_allowed' => 'string', // For filtering
'_social_security_required' => 'string', //For filtering
'_universal_application' => 'string', // For filtering
];
foreach ($meta_fields as $key => $type) {
register_post_meta('housing_listing', $key, [
'type' => $type,
'single' => true,
'show_in_rest' => [
'schema' => [ 'type' => $type ],
'auth_callback' => '__return_true',
],
]);
}
}
function n_register_housing_listing_cpt() {
$labels = array(
'name' => __( 'Housing Listings', 'hrdc-custom-tools' ),
'singular_name' => __( 'Housing Listing', 'hrdc-custom-tools' ),
'menu_name' => __( 'Housing Listings', 'hrdc-custom-tools' ),
'edit_item' => __( 'Edit Housing Listing', 'hrdc-custom-tools' ),
'view_item' => __( 'View Housing Listing', 'hrdc-custom-tools' ),
'search_items' => __( 'Search Listings', 'hrdc-custom-tools' ),
'not_found' => __( 'No housing listings found', 'hrdc-custom-tools' ),
'not_found_in_trash' => __( 'No housing listings found in Trash', 'hrdc-custom-tools' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'capability_type' => 'post',
'exclude_from_search' => true,
'map_meta_cap' => true,
'capabilities' => array(
'create_posts' => TRUE,
),
'menu_icon' => 'dashicons-building',
);
register_post_type( 'housing_listing', $args );
add_action( 'wp_enqueue_scripts', 'n_enqueue_frontend_scripts' );
function n_enqueue_frontend_scripts() {
// Enqueue your main front‑end script
wp_enqueue_script( 'n-frontend', plugin_dir_url( __FILE__ ) . 'housing-listings/edit.js', array(), null, true );
// Query for housing listing posts.
$query = new WP_Query( array(
'post_type' => 'housing_listing',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
) );
$listings_data = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
// Build an array of data for each listing.
$listings_data[] = array(
'property_name' => get_the_title(),
'address' => get_post_meta( $post_id, '_address', true ),
'city' => get_post_meta( $post_id, '_city', true ),
'county' => get_post_meta( $post_id, '_county', true ), // if registered
'property_manager' => get_post_meta( $post_id, '_property_manager', true ),
'phone' => get_post_meta( $post_id, '_phone', true ),
'website' => get_post_meta( $post_id, '_website', true ),
'category' => get_post_meta( $post_id, '_category', true ),
'reserved_for' => get_post_meta( $post_id, '_reserved_for', true ),
'application_fee' => get_post_meta( $post_id, '_application_fee', true ),
'felonies_considered'=> get_post_meta( $post_id, '_felonies_considered', true ),
'credit_check_not_required' => get_post_meta( $post_id, '_credit_check_not_required', true ),
'unit_types' => get_post_meta( $post_id, '_unit_types', true ),
'pets_allowed' => get_post_meta( $post_id, '_pets_allowed', true ),
'social_security_required' => get_post_meta( $post_id, '_social_security_required', true ),
'universal_application' => get_post_meta( $post_id, '_universal_application', true ),
'unique_id' => get_post_meta( $post_id, '_unique_id', true ),
'description' => get_the_content(),
);
}
wp_reset_postdata();
}
// Localize the data to your front‑end script.
wp_localize_script( 'n-frontend', 'hlData', $listings_data );
I have been building a custom word press plugin that takes in a json file and updates a list of posts with meta data. But I cant figure out how to pull the meta data. I have created some logs that show that the meta data is in fact being stored properly. I just can't figure out why it is not pulling.
Display, shows title, content, data but Post Meta is empty:
export default function HousingListingsBlockEdit( { attributes } ) {
const blockProps = useBlockProps();
// Retrieve housing_listing posts from the REST API.
const listings = useSelect( ( select ) => {
return select( 'core' ).getEntityRecords( 'postType', 'housing_listing', {
per_page: 1,
context: 'edit'
} ) || [];
}, [] );
return (
<div { ...blockProps }>
<div id="hl-results" style={ { maxWidth: '1000px', margin: '0 auto' } }>
{ finalListings.length ? (
finalListings.map( ( post ) => {
return (
<div { ...blockProps }>
{ post.id ? (
<div>
<p><strong>Post Name:</strong> { post.title.rendered }</p>
<p><strong>Post Date:</strong> { post.date }</p>
<p><strong>Post Description:</strong></p>
<div dangerouslySetInnerHTML={ { __html: post.content.rendered } } />
<p><strong>Post Meta:</strong></p>
<pre>{ JSON.stringify( post.meta, null, 2 ) }</pre>
</div>
) : (
<p>{ __( 'No listing found.', 'hrdc-custom-tools' ) }</p>
) }
</div>
);
})
) : (
<p>{ __( 'No listings found.', 'n-custom-tools' ) }</p>
) }
</div>
</div>
);
}
And in my render page I am getting the post meta as seen here:
$query = new WP_Query( array(
'post_type' => 'housing_listing',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
'cache_results' => false,
) );
$container_class = isset( $attributes['containerClass'] ) ? $attributes['containerClass'] : '';
$container_style = isset( $attributes['containerStyle'] ) ? $attributes['containerStyle'] : '';
$output = '<div class="n-housing-listings ' . esc_attr( $container_class ) . '" style="' . esc_attr( $container_style ) . '">';
$output .= '<div id="hl-results">';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
// Retrieve meta values once.
$address = get_post_meta( $post_id, '_address', true );
$city = get_post_meta( $post_id, '_city', true );
$manager = get_post_meta( $post_id, '_property_manager', true );
$phone = get_post_meta( $post_id, '_phone', true );
$website = get_post_meta( $post_id, '_website', true );
$category = get_post_meta( $post_id, '_category', true );
Further more I have localized the data in my main plugin file:
Function n_register_meta() {
$meta_fields = [
'_address' => 'string',
'_city' => 'string',
'_county' => 'string',
'_property_manager' => 'string',
'_phone' => 'string',
'_website' => 'string',
'_category' => 'string',
'_reserved_for' => 'string', // For filtering
'_application_fee' => 'string', // For filtering
'_felonies_considered' => 'string', // For filtering
'_credit_check_not_required' => 'string', // For filtering
'_unit_types' => 'string', // For filtering
'_pets_allowed' => 'string', // For filtering
'_social_security_required' => 'string', //For filtering
'_universal_application' => 'string', // For filtering
];
foreach ($meta_fields as $key => $type) {
register_post_meta('housing_listing', $key, [
'type' => $type,
'single' => true,
'show_in_rest' => [
'schema' => [ 'type' => $type ],
'auth_callback' => '__return_true',
],
]);
}
}
function n_register_housing_listing_cpt() {
$labels = array(
'name' => __( 'Housing Listings', 'hrdc-custom-tools' ),
'singular_name' => __( 'Housing Listing', 'hrdc-custom-tools' ),
'menu_name' => __( 'Housing Listings', 'hrdc-custom-tools' ),
'edit_item' => __( 'Edit Housing Listing', 'hrdc-custom-tools' ),
'view_item' => __( 'View Housing Listing', 'hrdc-custom-tools' ),
'search_items' => __( 'Search Listings', 'hrdc-custom-tools' ),
'not_found' => __( 'No housing listings found', 'hrdc-custom-tools' ),
'not_found_in_trash' => __( 'No housing listings found in Trash', 'hrdc-custom-tools' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'capability_type' => 'post',
'exclude_from_search' => true,
'map_meta_cap' => true,
'capabilities' => array(
'create_posts' => TRUE,
),
'menu_icon' => 'dashicons-building',
);
register_post_type( 'housing_listing', $args );
add_action( 'wp_enqueue_scripts', 'n_enqueue_frontend_scripts' );
function n_enqueue_frontend_scripts() {
// Enqueue your main front‑end script
wp_enqueue_script( 'n-frontend', plugin_dir_url( __FILE__ ) . 'housing-listings/edit.js', array(), null, true );
// Query for housing listing posts.
$query = new WP_Query( array(
'post_type' => 'housing_listing',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
) );
$listings_data = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
// Build an array of data for each listing.
$listings_data[] = array(
'property_name' => get_the_title(),
'address' => get_post_meta( $post_id, '_address', true ),
'city' => get_post_meta( $post_id, '_city', true ),
'county' => get_post_meta( $post_id, '_county', true ), // if registered
'property_manager' => get_post_meta( $post_id, '_property_manager', true ),
'phone' => get_post_meta( $post_id, '_phone', true ),
'website' => get_post_meta( $post_id, '_website', true ),
'category' => get_post_meta( $post_id, '_category', true ),
'reserved_for' => get_post_meta( $post_id, '_reserved_for', true ),
'application_fee' => get_post_meta( $post_id, '_application_fee', true ),
'felonies_considered'=> get_post_meta( $post_id, '_felonies_considered', true ),
'credit_check_not_required' => get_post_meta( $post_id, '_credit_check_not_required', true ),
'unit_types' => get_post_meta( $post_id, '_unit_types', true ),
'pets_allowed' => get_post_meta( $post_id, '_pets_allowed', true ),
'social_security_required' => get_post_meta( $post_id, '_social_security_required', true ),
'universal_application' => get_post_meta( $post_id, '_universal_application', true ),
'unique_id' => get_post_meta( $post_id, '_unique_id', true ),
'description' => get_the_content(),
);
}
wp_reset_postdata();
}
// Localize the data to your front‑end script.
wp_localize_script( 'n-frontend', 'hlData', $listings_data );
Share
Improve this question
asked Mar 24 at 18:05
user261073user261073
1
1
|
1 Answer
Reset to default 1After a lot of debugging, I found that I did not add support custom fields to the post register.
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ), --- solved the issue.
本文标签: pluginsWord Press CPT Post Meta data is not pulling
版权声明:本文标题:plugins - Word Press CPT Post Meta data is not pulling 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744344687a2601685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
house_listing
inside a standard core query loop block? There's no need to reinvent half the core block to lock down the query args, that's what variants/patterns are for – Tom J Nowell ♦ Commented Mar 24 at 18:47