Problem with custom post type, mysql table data and Bricks builder

Friends I have a problem with custom post fields and post meta.
I will describe what I have and what I want to achieve.

  1. I have a custom mysql table called wp_c2tarify and in it 27 records. I want to display the records from that table in bricks builder. And if possible to style them.

  2. I have come to the conclusion that custom post field is the easiest way. So I have a class where I call in the constructor
    add_action(‘init’, array($this, ‘register_tarify_post_type’)); //here I register a custom post type called tariffs

    /**

    • Register custom post type ‘tariff’
      */
      public function register_tarify_post_type() {
      $args = array(
      ‘public’ => true,
      ‘label’ => ‘Tariffs’,
      ‘supports’ => array(‘title’, ‘custom-fields’),
      ‘show_in_rest’ => true, // Enable in REST API for Gutenberg and potentially Bricks
      );
      register_post_type(‘tariff’, $args);
      }

In the admin, I created a new link to a post type called Tariffs. But as I said, the data I want to display from the wp_c2tarify table I don’t want to create the data directly in the wordpress tables.

  1. So I called filter method in constructor to create virtual data in custom post type tariff.
    // Filters to create virtual posts
    add_filter(‘the_posts’, array($this, ‘create_virtual_posts’), 10, 2);

    /**

    • Create virtual posts from data in the table

    • @param array $posts Array of posts returned by WordPress

    • @param WP_Query $query Query object

    • @return array Modified array of posts
      */
      public function create_virtual_posts($posts, $query)
      {
      global $wpdb;
      // Checks if the query is for posts of type ‘tariff’
      $post_type = $query->get(‘post_type’);

      if (is_array($post_type)) {
      if (!in_array(‘tariff’, $post_type))) {
      return $posts;
      }
      } elseif ($post_type !== ‘tariff’) {
      return $posts;
      }

      // Securely retrieve data from the database
      $tarify = $wpdb->get_results(“SELECT * FROM {$this->table_name}”);

      $virtual_posts = array();

      foreach ($tarify as $tarif) {
      if (!isset($tarif->id)) {
      continue; // Skip if no valid ID
      }

       // Create a virtual post object
       $post = new \stdClass();
       $post->ID = absint($tarif->id); // Ensuring that the ID is an integer
       $post->post_author = 1;
       $post->post_date = current_time('mysql');
       $post->post_date_gmt = current_time('mysql', 1);
       $post->post_title = sanitize_text_field($tarif->TARIF);
       $post->post_content = ''; // Empty content if not needed
       $post->post_excerpt = ''; // Empty excerpt
       $post->post_status = 'publish'; // Post status (e.g. publish)
       $post->post_type = 'tariff'; // Post type
       $post->comment_status = 'close'; // Comments open or closed
       $post->ping_status = 'open'; // Ping status
       $post->post_password = ''; // No password
      
       $virtual_posts[] = $post;
      

      }

      // If there are no virtual posts, return the original post array
      if (empty($virtual_posts)) {
      return $posts;
      }

      return $virtual_posts; // Combination of original and virtual posts
      }

So far everything works as it should. In the admin, under the Tariffs link, I can see 27 posts, titles. I don’t want to edit them, I just want to list them using custom post fields in Bricks builder.
And here I came across

  1. When I went to edit the TestPost page in Bricks builder I inserted a block in which I turned on the query loop, selected the post type and post type tariffs that are there, then I added a heading to the block for which I selected dynamic data and selected post_title.
    But after saving and reloading the page, nothing showed up. No 27 headings.

I found that if I remove some of the code from the create_virtual_posts method to create a viral post, the tariff type posts will show up on the frontend, but it totally breaks the Bricks preview of the page. So it’s obviously crap, but I’ve sacrificed , that probably the create_virtual_posts filter in question is called.

I deleted this (temporarily)
// Checks if the query is for posts of type ‘tariff’
$post_type = $query->get(‘post_type’);

    if (is_array($post_type)) {
        if (!in_array('tariff', $post_type))) {
            return $posts;
        }
    } elseif ($post_type !== 'tariff') {
        return $posts;
    }

/////

I know it’s long but I’m not really sure what to do anymore. It’s clear to me that I’m doing something wrong, but I don’t know what. Thanks for any advice.
Alternatively, links to some working tutorial where the same problem is solved.
Thanks again for your helpfulness and patience.