• I have a CPT called Agenda created in PODS. It is stored in a table. I need to order the results of the query by a date type custom field. But I can’t in my query editor (I work with Bricks Builder) set that date field as a meta key. Is there any way to solve this? I mean relate this date field in some way so that the orderby works. Could the storage system be changed from table to meta? If I undertake this task, where would I save the values ​​of my Agenda table? Thank you so much

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Scott Kingsley Clark

    (@sc0ttkclark)

    You could use https://github.com/pods-framework/pods-convert for this. It’s a plugin that adds a new WP-CLI command that you can use to convert a pod to a new content type or content storage option.

    For CPTs, converting from table-based storage to meta-based storage will bring over all of those fields from the table for each post (1 row per post) into the postmeta table (1 row per field value per post).

    Thread Starter mlsaura

    (@mlsaura)

    Hi.
    Thanks.
    I try this 🙂

    Thread Starter mlsaura

    (@mlsaura)

    Hi! I have installed the plugin. I also connect with it to the hosting using the SSH protocol and access the web files through my terminal. Once there, I enter the WP CLI, but from there I don’t clearly see how to continue. I give the command wp pods-convert migrate --pod=agenda (agenda is the name of the pod whose storage I want to change from table to meta), but nothing happens. Can you guide me a little, please? Thank you

    Plugin Author Scott Kingsley Clark

    (@sc0ttkclark)

    Try adding --debug to that command to see if there’s any other output happening. Can you provide that?

    Plugin Support Paul Clark

    (@pdclark)

    When I attempt to run the CLI command with an ACT called agenda, I get the error Pod unable to be deleted.

    Considering the original question and context of Bricks Builder, it may be the case that the entire Pod is not stored in a table (Advanced Content Type), but rather it is using table storage for the post meta. Otherwise, the post type would not show up in the Bricks Builder query editor at all — not just the field.

    If I run the command on a Post Type with Table-based Meta storage:

    wp pods-convert migrate --pod=agenda_old --new_storage=meta --new_name=agenda_new

    The output is:

    Found pod agenda_old (#133 - post_type) to migrate.
    Setting up new pod agenda_new2 (post_type).
    Error: Error: Pod agenda_new2 already exists

    …there is no Pod agenda_new2 and there was no 2.

    Below is code for a WP CLI command that may work for your use case if your existing Pod agenda is a Post Type using Table Storage for meta, not an Advanced Content Type.

    To use the command:

    • Back up the database. For example, with wp db export > db.sql or with PHPMyAdmin.
    • Create a new Pod using Post Meta storage, for example titled agenda_post.
    • Create all fields on the new Pod, using the same configuration as agenda. All names should be the same. If there are many fields, it may be faster to use Pods > Components > Import/Export Packages to export a configuration for agenda, edit that text file for the Pod name and storage type, then import as a new Pod.
    • Run the command wp pods migrate-to-postmeta --from=agenda --to=agenda_post
    • The command expects that fields are the same, and Post Titles are not repeated. If the command is run multiple times, it will skip copying over items with the same Post Title.
    <?php
    /**
    * Plugin Name: Pods CLI Migrate to Postmeta
    * Description: Migrate a Post Type using Table Storage for meta to a new Pod using postmeta storage. Requires that the new pod has been created, and that all field names and configurations are the same. Skips duplicate titles. Usage: <code>wp pods migrate-to-postmeta --from=pod_name_using_table_storage_for_meta --to=pod_name_using_postmeta</code>
    * Plugin URI: https://wordpress.org/support/topic/set-meta-key-for-a-query-that-is-based-on-a-cpt-stored-in-a-table/
    * Version: 1
    */
    add_action(
    'cli_init',
    function() {
    \WP_CLI::add_command(
    'pods migrate-to-postmeta',
    function( $a, $aa ) {
    global $wpdb;

    $from = $aa['from'];
    $to = $aa['to'];

    if ( empty( $to ) || empty( $from ) ) {
    \WP_CLI::log( 'Please set --to post type and --from post type. All fields should already be configured.' );
    exit;
    }

    // Query all the 'from' posts.
    $from_pod = pods(
    $from,
    [
    'limit' => -1,
    'where' => 't.post_status IS NOT NULL',
    ]
    );
    // Reference the 'new' Pods post_type object.
    $to_pod = pods( $to );

    // Loop through the 'from' posts.
    while ( $from_pod->fetch() ) {
    // Get field names.
    $fields = $from_pod->fields();
    // Get values for each field.
    $field_values = array_reduce(
    array_keys( $fields ),
    function( $carry, $field_key ) use ( $from_pod, $fields ) {
    // Whether the field is a single value or not.
    $is_singular = ( 'single' === $fields[ $field_key ]->args['pick_format_type'] );
    // Get the field value unless it's ID or post_type.
    $new_field = ( in_array( $field_key, [ 'ID', 'post_type' ] ) )
    ? []
    : [ $field_key => $from_pod->field( $field_key, $is_singular, true ) ];
    return array_merge(
    $carry,
    $new_field
    );
    },
    []
    );
    // Check of a 'to' post already exists with the same title.
    $sql = sprintf(
    "SELECT ID FROM %s WHERE post_title = '%s' AND post_type = '%s'",
    $wpdb->posts,
    esc_sql( $field_values['post_title'] ),
    esc_sql( $to )
    );
    $existing_new_id = (int) $wpdb->get_var( $sql );

    if ( empty( $existing_new_id ) ) {
    // The title does not already exist. Create it.
    $new_id = $to_pod->save( $field_values );
    // Log the new ID and edit link.
    \WP_CLI::log(
    sprintf(
    'Saved new ID %d for 「%s」 at %s',
    $new_id,
    $field_values['post_title'],
    add_query_arg(
    [
    'post' => $new_id,
    'action' => 'edit',
    ],
    admin_url( 'post.php' )
    )
    )
    );
    }else {
    // The title already exist. Log the edit link and do nothing.
    \WP_CLI::log(
    sprintf(
    'Skipped existing ID %d for 「%s」 found at %s',
    $existing_new_id,
    $field_values['post_title'],
    add_query_arg(
    [
    'post' => $existing_new_id,
    'action' => 'edit',
    ],
    admin_url( 'post.php' )
    )
    )
    );
    }
    }
    }
    );
    }
    );
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.