Title: Outside Query Loop
Last modified: April 11, 2023

---

# Outside Query Loop

 *  [Vadim](https://wordpress.org/support/users/vakrug/)
 * (@vakrug)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/)
 * After spending WEEKS trying to do the most simple thing in WordPress I could 
   imagine, I still failed and now I write here.
 * I am using the latest WordPress version (6.2) right now. As I understand, full
   site editor is a modern way to create pages today. I studied theme “twentytwentythree”
   and I came into conclusion , that “patterns” folder is the only place where I
   can write custom PHP code (and I want to write custom PHP code).
 * What have I done: I created custom post type (inside plugin, but that is irrelevant
   now) called “objectonmap”. Then I created custom fields for that custom post 
   type using “metaboxes”. Fields are “lat” and “lon”. It worked! I was able to 
   create new custom posts, fill custom fields and all of this was saved in DB, 
   indeed! May be WordPress is not that horrific as I thought earlier? Now I want
   to output those new fields on an actual page. Right now I have only “index.html”
   inside “templates” folder. I used “WordPress Site Editor” to edit “Index” template.
   I added “Query Loop” using “Block Inserter” and selected “objectonmap” post type
   in “settings”. It worked! I was able to see a list of “objectonmap”, their “title”
   and “excerpt”.
 * Now I want to see “lat” and “lon” fields. Since I don’t want to just output their
   values, but in the future I want to draw a map, I created custom part “coordinates.
   html” inside “parts”. Inside was only this:
 *     ```wp-block-code
       <!-- wp:pattern {"slug":"objectsonmap/coordinates"} /-->
       ```
   
 * Then I created “coordinates.php” file inside “patterns” folder. For testing purposes
   I included only echo “Hello, world” there. Then I opened “WordPress Site Editor”
   and added newly created “coordinates” block inside existing “Query Loop” block
   in “Index” template. Again, it worked! I can see “Hello, world” next to every“
   objectonmap” post on a page.
 * Real problem starts here. **There is no way I can access post ID inside my custom
   pattern!** I am supposed to be inside “Query Loop”, because I placed my block
   inside it, but I am not. “in_the_loop()” returns false. “global $wp_query; $wp_query-
   >post” returns null. “get_post_meta” function do return what I want, but only
   if I provide the correct post ID, which I cannot get no matter what I do! Also
   tried “get_queried_object_id()” and “get_post()” functions with no success.
 * So what I am doing wrong? Why I am outside “Query Loop”?
    -  This topic was modified 3 years, 1 month ago by [Vadim](https://wordpress.org/support/users/vakrug/).
    -  This topic was modified 3 years, 1 month ago by [Vadim](https://wordpress.org/support/users/vakrug/).

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16645929)
 * TBH I’m not sure what the context is within a query loop. I suggest you try `
   get_the_ID()`. AFAIK it should work in any legitimate WP loop context. Certain
   PHP loops have been created where it will not work. In such a case you’d need
   to look at the loop’s source code to learn how to get the current post object’s
   ID.
 *  [Daniel Richards](https://wordpress.org/support/users/talldanwp/)
 * (@talldanwp)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16647113)
 * I’m not sure that functions like `get_the_ID` and `in_the_loop` will work within
   Query Loop child blocks. [There’s some background in this issue](https://github.com/WordPress/gutenberg/issues/35676).
 * The main problem with those functions is that they’re PHP only, and the block
   editor typically uses a mixture of JavaScript and PHP.
 * For blocks, there’s a feature called ‘block context’, and this can be used to
   get the post id and type within a query loop.
 * This probably means that you’ll need to start down the path of developing a block,
   which probably sounds like a lot. I don’t think it’s too complicated though, 
   and you can primarily use PHP for a block if that’s what you’re most comfortable
   with. Probably the hardest thing is getting the boilerplate for your block right
   if you don’t want to use too much JavaScript tooling.
 * A simple block like Post Title, might act as a good reference for block context.
   [Here’s a link to the code for that block.](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/post-title)
 * [The first part of block context is to declare what properties your block consumes in the block.json.](https://github.com/WordPress/gutenberg/blob/3317ba195da0149d0bae221dc3516cd76f536c5d/packages/block-library/src/post-title/block.json#L9)
 * [In the PHP code for your block, those properties are received in a parameter of the render callback function](https://github.com/WordPress/gutenberg/blob/3317ba195da0149d0bae221dc3516cd76f536c5d/packages/block-library/src/post-title/index.php#L17-L23).
   That render function returns the HTML for your block, but in that function you
   can do just about anything you want.
 * You’ll also need to build an editor interface for your block. [The most minimal option for a PHP developer is to use the wp.serverSideRender component](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-server-side-render/),
   which will display your PHP render callback in the editor. You could also build
   a way to edit the latitude and longitude in your block.
 * Some docs that may be useful:
    - [Extending the Query Loop](https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/extending-the-query-loop-block/)
    - [Creating Dynamic Blocks](https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/creating-dynamic-blocks/)
    - [Block Context](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-context/)
 *  Thread Starter [Vadim](https://wordpress.org/support/users/vakrug/)
 * (@vakrug)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16647147)
 * get_the_ID() returned nothing.
 * Thanks for replies. OK, I understand, that I must create a custom block. I will
   report about my success/failure.
 *  [Aaron Robertshaw](https://wordpress.org/support/users/aaronrobertshaw/)
 * (@aaronrobertshaw)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16650367)
 * A quick note regarding `wp.serverSiderRender`, the [docs](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-server-side-render/)
   state the following:
 * > ServerSideRender should be regarded as a fallback or legacy mechanism, it is
   > not appropriate for developing new features against.
 * The recommendation there is to actually build your new block in conjunction with
   any necessary REST API endpoint so Javascript is used for rendering the client-
   side edit component.
 *  Thread Starter [Vadim](https://wordpress.org/support/users/vakrug/)
 * (@vakrug)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16675426)
 * I somehow managed to create a block that uses context and is even able to output
   on screen something. But I still have no idea how to get postId (and what to 
   do with it after, if I somehow get one).
 * My block uses context provided by Query Block: “queryId”, “query”, “queryContext”.
   None of this contains data I need. How do you get data stored in DB out of this
   objects?
 * Also I tried to research code and found some very strange stuff. I suspect that“
   wp-includes/blocks” folder contain wordpress blocks. But inside each block folder
   there are only .css files and block.json file. Not a single .js files of .php
   files like in “[https://github.com/WordPress/gutenberg/tree/3317ba195da0149d0bae221dc3516cd76f536c5d/packages/block-library/src/post-template&#8221](https://github.com/WordPress/gutenberg/tree/3317ba195da0149d0bae221dc3516cd76f536c5d/packages/block-library/src/post-template&#8221);
   for example. Where is all the logic? And what is “style”: “wp-block-post-template”?
   I thought there should be a path to .css file, but what is “wp-block-post-template”.
   All of this is so different to what I read in “[https://developer.wordpress.org/block-editor/getting-started/create-block/&#8221](https://developer.wordpress.org/block-editor/getting-started/create-block/&#8221);
   tutorial. There is a “index.php” file inside “post-template” folder inside “gutenberg”
   package. But I expected “post-title.php” file.
 * May be too late to ask, but what is a REAL way to learn WordPress? Right now 
   I believe that “[https://developer.wordpress.org/&#8221](https://developer.wordpress.org/&#8221);
   and “[https://wordpress.org/documentation/&#8221](https://wordpress.org/documentation/&#8221);
   is a joke and completely inappropriate to begin with.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16678958)
 * Everyone learns differently, there’s no single right answer. One effective way
   to learn a specific topic for many is to write the documentation or a tutorial
   for it. You end up doing a lot of research and investigation, learning along 
   the way.
 * WP documentation has been assembled by a team of volunteers. No one would argue
   that it works well for everyone. You can contribute to making it better if you’re
   so inclined. Ways you can contribute are outlined on [the doc team’s “make” page](https://make.wordpress.org/docs/).
 *  [Daniel Richards](https://wordpress.org/support/users/talldanwp/)
 * (@talldanwp)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16687511)
 * > My block uses context provided by Query Block: “queryId”, “query”, “queryContext”.
   > None of this contains data I need. How do you get data stored in DB out of 
   > this objects?
 * I think you want to use the `postId` property from the block context.
 * How you fetch the post data is different different depending on whether you’re
   writing JS or PHP. In PHP, you can use `get_post( $context[ 'postId' ] )`.
 * For the JS part, the way I’d recommend is `useEntityProp( 'postType', postType,
   property , postId )`, which provides an API for both reading from and writing
   to the properties of a post. There’s an example here – [https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/post-title/edit.js#L40](https://github.com/WordPress/gutenberg/blob/832da5008b68ae4c3fef9acc87928f44ee4f18d8/packages/block-library/src/post-title/edit.js#L40)
 * I’d still recommend starting with `ServerSideRender` for the JS part to make 
   the learning curve easier. It’s often easier to get something working first, 
   and then build on the initial concepts.
 * > Also I tried to research code and found some very strange stuff. I suspect 
   > that “wp-includes/blocks” folder contain wordpress blocks. But inside each 
   > block folder there are only .css files and block.json file.
 * The source code for blocks is in the gutenberg project, in the `packages/block-
   library` folder.
 *  Thread Starter [Vadim](https://wordpress.org/support/users/vakrug/)
 * (@vakrug)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16687852)
 * > I think you want to use the `postId` property from the block context.
 * There is no “postId” in “providesContext” section of the “query” block. And also
   there is no “providesContext” section in “post-template” at all, whish confuses
   me even more.
 * > The source code for blocks is in the gutenberg project, in the `packages/block-
   > library` folder.
 * This explains nothing. Where is code for blocks inside my WordPress project? 
   Even minimized or something. I can see only “.css” files and “block.json”.
 * OK, I understood that outputting a freaking number from a DB to a web page is
   an extremely complex task in WordPress. Definitely not for beginners, however
   I cannot figure out what can be even more basic. At this point I am very tempted
   to just forget about WordPress like a nightmare. But before that I will try to
   do at least something…
 *  [Daniel Richards](https://wordpress.org/support/users/talldanwp/)
 * (@talldanwp)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16688022)
 * Maybe we’ll leave it there if this is how you reply to people trying to help 
   you.
 * Good luck!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Outside Query Loop’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 9 replies
 * 4 participants
 * Last reply from: [Daniel Richards](https://wordpress.org/support/users/talldanwp/)
 * Last activity: [3 years, 1 month ago](https://wordpress.org/support/topic/outside-query-loop/#post-16688022)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
