Support

Account

Home Forums General Issues Saving post content into product meta field

Solving

Saving post content into product meta field

  • I have Books as Products in woocommerce and chapters as posts in wordpress blog. For example, “Song of Ice and Fire” is a woocommerce product while “A Clash of the Kings” is a post in a category in wordpress blog.

    The only information A Clash of the Kings has in the content is link to the amazon website.

    Now, I want to grab the content (more or less links) from each post within a given wordpress category and save them into the repeater field in my Product page.

    I’m stuck as I can’t get how to save the content into repeater meta fields. Is there anyone who could help me out?
    ************
    This is the code

    <?php
    $scat = get_the_term_list( $id_product,”scat”); /* I add this ‘scat’ through the product page in Admin Panel by using Custom Taxonomy. It will be the ID of the wordpress category */

    // query
    $query = new WP_Query( array( ‘cat’ => $scat ) );

    ?>
    <?php if( $the_query->have_posts() ): ?>

      <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>

      <?php the_content(); ?> /* Now I’m stuck here */

      <?php endwhile; ?>

    <?php endif; ?>

    <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

  • update*
    it still doesn’t work thou

    <?php
    $scat = get_the_term_list( $id_product,”scat”); // I add this ‘scat’ through the product page in Admin Panel by using Custom Taxonomy. It will be the ID of the wordpress category */

    // query
    $query = new WP_Query( array( ‘cat’ => $scat ) );

    ?>
    <?php if( $the_query->have_posts() ): ?>

    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>

    $content = apply_filters(‘the_content’, $post->post_content);

    <?php add_post_meta($post_id, ‘chapter’, $content); ?>

    <?php endwhile; ?>

    <?php endif; ?>

    <?php wp_reset_query(); ?>

  • Here’s some directions for ya

    1) the function get_the_term_list() returns the HTML https://codex.wordpress.org/Function_Reference/get_the_term_list. the function you are looking for is get_the_terms() https://developer.wordpress.org/reference/functions/get_the_terms/

    2) when you initialize the WP_Query, take a look at what types of parameter you should pass to search in the category https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters. ‘category__in’ requires an array of term ids. And ‘cat’ requires a term id.

    3) use get_the_content() to retrieve the post content https://developer.wordpress.org/reference/functions/get_the_content/

    4) to update repeater field, you can either use update_field or update_sub_field functions.
    https://www.advancedcustomfields.com/resources/update_field/
    https://www.advancedcustomfields.com/resources/update_sub_field/

    these should get you going with what you are trying to do.

    Cheers!

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

The topic ‘Saving post content into product meta field’ is closed to new replies.