Forum Replies Created
-
AuthorPosts
-
Fergal
ParticipantHey Long,
Ok I see now. Thanks for that information.
September 29, 2022 at 1:15 AM in reply to: ✅Custom Field PHP Code: Text Domain and Function Name #38467Fergal
ParticipantVery helpful Long, thank you for that information.
Fergal
ParticipantGreat thank you
Fergal
ParticipantHey 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,
FergalFergal
ParticipantNevermind, 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; } } }Fergal
ParticipantHey 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>'; } } }Fergal
ParticipantHey 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 }Fergal
ParticipantHey 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/G3rEmkGHowever, 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,
FergalFergal
ParticipantHey 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,
FergalFergal
ParticipantThank you Long!
-
AuthorPosts