Friends I have a problem with custom post fields and post meta.
I will describe what I have and what I want to achieve.
-
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.
-
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);
}
- Register custom post type âtariffâ
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.
-
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
- 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.