Post Meta Custom Field inside Query Loop block

Support Meta Box AIO Post Meta Custom Field inside Query Loop blockResolved

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #38190
    FergalFergal
    Participant

    Hey there,

    I have a few custom fields for my posts that I would like to include in the query loop block in Gutenberg.

    How is this achieved?

    Thanks,
    Fergal

    #38194
    Long NguyenLong Nguyen
    Moderator

    Hi Fergal,

    In the callback function (or template), you can use the passing variable $post_id to get the field value of the current post. Then use the field value in the query args, for example:

    function my_hero_callback( $attributes, $is_preview = false, $post_id = null ) {
        $field_value = rwmb_meta( 'field_id', '', $post_id );
        $args = array(
            'meta_key'   => 'field_id',
            'meta_value' => $field_value
        );
        $query = new WP_Query( $args );
    }

    Read more on the documentation https://docs.metabox.io/extensions/mb-blocks/#render_callback
    https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

    #38199
    FergalFergal
    Participant

    Hey Long,

    Thanks for your reply and help.

    Using your information I've created a callback function in functions.php

    I tried to adapt it to get two field values per post (company name and location). Would this work?

    
    function my_featured_callback( $attributes, $is_preview = false, $post_id = null ) {
        $company_name_value = rwmb_meta( 'company_name', '', $post_id );
        $location_value = rwmb_meta( 'location', '', $post_id );
        $args = array(
            'post_type' => 'job-opening',
             'tax_query' => array(
                array(
                    'taxonomy' => 'department',
                    'field'    => 'slug',
                    'terms'    => 'featured-jobs',
                ), 
            'meta_key'   => array('company_name', 'location')
            'meta_value' => array($company_name_value, $location_value)
        );
        $query = new WP_Query( $args );
    

    Next, how would I get this to display in the Gutenberg Query Loop? Or should I create a new block that can be inserted into the gutenberg editor?

    Thanks again,
    Fergal

    #38201
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Yes, you should create a custom block to display the list of posts with more parameters. The block Query Loop of WordPress only supports adding some simple settings (post type, featured image, color ...)

    #38202
    FergalFergal
    Participant

    Hey Long,

    Thanks for the guidance. I've created the block and placed it in the editor as seen in the following images.
    https://imgur.com/G3rEmkG

    However, nothing shows up on the website. For a custom post type I would like to see:

    • post title
    • company_name
    • location
    • jo_job_type
    • jo_comp_logo
    • date_posted

    Where everything but the post title are custom fields. I believe I need to still create a way to display this data and to style it. Can you please tell me what I am missing still? Apologies for my lack of expertise in this area.

    Thanks,
    Fergal

    #38209
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Can you please share the callback function that you use to display the list of posts?

    #38216
    FergalFergal
    Participant

    Hey Long,

    Yes please see it below.

    
    // function for featured jobs and custom fields
    function my_featured_callback( $attributes, $is_preview = false, $post_id = null ) {
        $field_value = rwmb_meta( 'field_id', '', $post_id );
        $args = array(
            'meta_key'   => 'field_id',
            'meta_value' => $field_value,
            'post_type'  => 'job-opening',
            'tax_query' => array(
                array(
                    'taxonomy' => 'department',
                ), 
            ),
            // filter to posts where switch = 1
            'key' => 'featured_job',
            'value' => 1,
            'compare' => '=',
            'type' => 'NUMERIC',
        );
    
        $query = new WP_Query( $args );
        
        // The Loop
       while ( $query->have_posts() ) {
    	    $query->the_post();
            echo 'the loop is working';
    	    echo '<li>' . get_the_title() . '</li>';?>
            
            <p>
                <strong>Company:</strong> <?= rwmb_get_value( 'company_name' ) ?>
            </p>
            <p>
                <strong>Location:</strong> <?= rwmb_get_value( 'location' ) ?>
            </p>
            <p>
                <strong>Job Type:</strong> <?= rwmb_get_value( 'jo_job_type' ) ?>
            </p>
            <p>
                <strong>Company Logo:</strong> <?= rwmb_get_value( 'jo_comp_logo' ) ?>
            </p>
            <p>
                <strong>Date Posted:</strong> <?= rwmb_get_value( 'date_posted' ) ?>
            </p>
            <?php 
        }
    
        wp_reset_postdata();
    }
    

    I also came across this https://docs.metabox.io/extensions/mb-custom-table/#query-posts-with-wp_query and my custom fields should be in a custom table. So in this case, is the following format preferred? First get the posts where featured_job = 1

    
    global $wpdb;
    $ids = $wpdb->get_col( "SELECT ID FROM your_table WHERE featured_job = 1" );
    
    if ( empty( $ids ) ) {
        echo 'There is no posts';
    } else {
        $query = new WP_Query( [
            'post_type' => 'job-opening',
            'post__in'  => $ids,
        ] );
        // Do something
    }
    
    #38227
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Yes, correct. If the custom field value is stored in the custom table, please follow this documentation to get posts by field value https://docs.metabox.io/extensions/mb-custom-table/#query-posts-with-wp_query

    #38235
    FergalFergal
    Participant

    Hey Long,

    Great we are making progress! Thanks for your help.

    The callback below is now printing out the 2 lines for each featured post of job_openings post type. The callback is firing from a block I made using MB Blocks.

    Can you please tell me now how to access the custom fields for my featured posts? Let's say I have these custom field IDs that I want to get the value from: "company_name", "location", "jo_job_type".

    function my_featured_callback( $attributes, $is_preview = false, $post_id = null ) {
        global $wpdb;
        $ids = $wpdb->get_col( "SELECT ID FROM job_openings WHERE featured_job = 1" );
    
        if ( empty( $ids ) ) {
            echo 'There are no posts';
        } else {
            $query = new WP_Query( [
                'post_type' => 'job-opening',
                'post__in'  => $ids,
            ] );
            // Do something
                // The Loop
            while ( $query->have_posts() ) {
                $query->the_post();
                echo 'the loop is working';
                echo '<li>' . get_the_title() . '</li>';
            }
        }
    }
    #38236
    FergalFergal
    Participant

    Nevermind, the below is working! Is there a way to choose which fields I want to display in the Block? My goal is to replicate how the Query Loop block works in the editor so that I can insert each custom field value into its own container.

    function my_featured_callback( $attributes, $is_preview = false, $post_id = null ) {
        global $wpdb;
        $ids = $wpdb->get_col( "SELECT ID FROM job_openings WHERE featured_job = 1" );
    
        if ( empty( $ids ) ) {
            echo 'There are no posts';
        } else {
            $query = new WP_Query( [
                'post_type' => 'job-opening',
                'post__in'  => $ids,
            ] );
            // Do something
            // The Loop
            while ( $query->have_posts() ) {
                $query->the_post();?>
                <h2><?the_title(); ?></h2>
                <?
                $company_name_value = rwmb_meta( 'company_name', '', get_the_ID() );
                $location_value = rwmb_meta( 'location', '', get_the_ID() );
                $jo_job_type_value = rwmb_meta( 'jo_job_type', '', get_the_ID() );
                $date_posted_value = rwmb_meta( 'date_posted', '', get_the_ID() );
    
                echo $company_name_value;
                echo $location_value;
                echo $jo_job_type_value;
                echo $date_posted_value;
            }
        }
    }
    
    #38254
    FergalFergal
    Participant

    Hey Long,

    Can you please help me to get pagination to work when using this method of building the $query where I'm querying a custom post type from a custom table?

    global $wpdb;
    $ids = $wpdb->get_col( "SELECT ID FROM your_table WHERE field1 = 'value1'" );
    
    if ( empty( $ids ) ) {
        echo 'There is no posts';
    } else {
        $query = new WP_Query( [
            'post_type' => 'post',
            'post__in'  => $ids,
        ] );
        // Do something
    }

    Thanks,
    Fergal

    #38267
    Long NguyenLong Nguyen
    Moderator

    Hi Fergal,

    Please follow the WP documentation to know how to display the pagination in a loop https://developer.wordpress.org/themes/functionality/pagination/

    #38269
    FergalFergal
    Participant

    Great thank you

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.