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 1. Have you registered the meta? 2. It's not clear if it's working in PHP but not in your edit component, or both, or neither, can you edit your question to state unambiguously? 3. You mentioned updating a list, are you trying to modify posts/meta or is this just another way of saying "render/display"? Finally 4. wouldn't this make more sense as a block displaying a single 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
Add a comment  | 

1 Answer 1

Reset to default 1

After 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