Support

Account

Home Forums Feature Requests Get custom field inside query loop block

Solving

Get custom field inside query loop block

  • I don’t think there is an existing feature request for this here.

    I 100% think this ought to be fixed in core per https://github.com/WordPress/gutenberg/issues/35676 and other similar issues/tickets.

    But, since that seems to not be happening. We need a very EASY way to reliably output custom field data inside WP’s query loop block.

    “shortcodes” are a horrible solution.

  • @jb510 were you able to find a solution for this? I keep getting the same value for all the posts. I was able to make it work a long time ago and I don’t even know what I did.

    Thank you.

  • @carlbtmn As I recall the shortcode hack worked… BUT… I think (not certain) that hack stopped working after the WP 6.2.1 security patch for an Unauthenticated Shortcode Execution… and I’m not clear if 6.2.2 restored it or left it still broken.

    Anyway, I’d say this is still open, and re-read tat GitHub issue for options to try.

  • Hi Guys – just want to share my workaround idea here…
    assuming you want to place a custom field value before (or after) the post-title block in the loop template, you can give the block a unique classname like in my case “company” and then look for that classname in the following code you need to put in your functions.php

    add_filter( 'render_block_core/post-title', function( $block_content, $block ) {
    	if(!empty($block["attrs"]["className"]) && $block["attrs"]["className"] != "company") return $block_content;
    		
    	return sprintf("<div style='margin-bottom:%s'><strong>%s</strong></div>%s","10px",get_field("company"),$block_content);
    }, 10, 2 );

    thats a pretty robust solution because it uses core code. In my case i need to output the custom field “company” before the post title in of one post in the loop. This will work for the frontend only!

  • @nicmare in the last 2 days i’ve tried getting caught up on where things stand with query block custom field shortcode status, found this post, and your solution cleared things up for me on this. thanks.

  • @nicmare This worked great to get the contents of a text field output right after a title. But now I want to have multiple injections that show up in different places.

    I modified your code to combine the items. It throws no errors, but when I used your code to output one item, then the other, separately, they worked. After combining them, it no longer works. I wonder if you or anyone else could help.

    
    add_filter( 'render_block_core/post-title', function( $block_content, $block ) {
    	if(!empty($block["attrs"]["className"]) && $block["attrs"]["className"] == "duration") {
    		return sprintf("<div style='margin-bottom:%s'><strong>%s</strong></div>%s","10px",get_field("clip_duration"),$block_content);
    	} else if (!empty($block["attrs"]["className"]) && $block["attrs"]["className"] == "readAuthor") {
    		$author = get_field('author');
    		foreach( $author as $theAuthor ) {
    			$authorName = get_the_title( $theAuthor->ID );	
    		}
    		return sprintf("<h4>Read / %s</h4>",$authorName,$block_content);
    	} else {
    		return $block_content;
    	}
    }, 10, 2 );
    
  • what do you think what i am? A machine? I can not read your comment / code. So i can not help you. But placing custom content before/after something is just like changing the order in the code. code for outputting the acf field after the title:

    add_filter( 'render_block_core/post-title', function( $block_content, $block ) {
    	if(!empty($block["attrs"]["className"]) && $block["attrs"]["className"] != "company") return $block_content;
    		
    	return sprintf("%s<div style='margin-top:%s'><strong>%s</strong></div>",$block_content,"10px",get_field("company"));
    }, 10, 2 );
  • @nicmare I apologize. Completely my mistake on the formatting.

    add_filter( 'render_block_core/post-title', function( $block_content, $block ) {
    	if(!empty($block["attrs"]["className"]) && $block["attrs"]["className"] == "duration") {
    		return sprintf("<div style='margin-bottom:%s'><strong>%s</strong></div>%s","10px",get_field("clip_duration"),$block_content);
    	} else if (!empty($block["attrs"]["className"]) && $block["attrs"]["className"] == "readAuthor") {
    		$author = get_field('author');
    		foreach( $author as $theAuthor ) {
    			$authorName = get_the_title( $theAuthor->ID );	
    		}
    		return sprintf("<h4>Read / %s</h4>",$authorName,$block_content);
    	} else {
    		return $block_content;
    	}
    }, 10, 2 );
  • Maybe I’m not understanding but why wouldn’t you just make an ACF block that pulls in what you need and they in the block editor nest that block inside the Query block or Post Temolate block?

  • because acf does not passes the correct post id of each post in the query. instead it returns the post id of the query loop post. every post in that loop get the same id. therefore it won’t work as get_field always tries to return the values of the loop containing post.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.