Support

Account

Home Forums Add-ons Repeater Field post content into repeater fields

Solving

post content into repeater fields

  • 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?
    ************

    <?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(); ?>

  • update*

    <?php
    $scat = get_the_term_list( $id_product,”scat”);
    $args = array(
    ‘post_type’ => ‘post’,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘category’,
    ‘terms’ => array( $scat ),
    ),
    );
    // query
    $query = new WP_Query( array( ‘cat’ => $args ) );

    ?>
    <?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_product_meta($product_id, ‘chapter’, $content); ?>

    <?php endwhile; ?>

    <?php endif; ?>

    <?php wp_reset_query(); ?>

    ******
    it still doesn’t work 🙁

  • more update*

    <?php
    $scat = get_the_term_list( $id_product,”scat”);
    $args = array(
    ‘post_type’ => ‘post’,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘category’,
    ‘terms’ => array( $scat ),
    ),
    );
    // query
    $query = new WP_Query( array( ‘cat’ => $args ) );

    ?>
    <?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

    if( have_rows(‘chapter’) ) {

    while( have_rows(‘chapter’) ) {

    the_row();

    $row = array(
    ‘link’ => $content ,
    );

    add_sub_row(‘link’, $row);

    }

    }

    ?>

    <?php endwhile; ?>

    <?php endif; ?>

    <?php wp_reset_query(); ?>

  • Not sure if you have this solved or not, the code is hard to follow in the format you posted.

    If your add_sub_row(‘link’, $row); call is not updating the field try using the field keys instead of the field names for your loop. When data for an ACF field does not already exist using the field names when updating fields will not work correctly.

  • <?php
    $scat = get_the_term_list( $id_product,”scat”); // I add scat taxonomy for each product to manually publish the Category ID of the blog
    
    $args = array(
    ‘post_type’ => ‘post’,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘category’,
    ‘terms’ => array( $scat ),
    ),
    );
    // query
    $query = new WP_Query( array( ‘cat’ => $args ) );
    
    ?>
    <?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
    
    if( have_rows(‘chapter’) ) {
    
    while( have_rows(‘chapter’) ) {
    
    the_row();
    
    $row = array(
    ‘link’ => $content ,
    );
    
    add_sub_row(‘link’, $row);
    
    }
    
    }
    
    ?>
    
    <?php endwhile; ?>
    
    <?php endif; ?>
    
    <?php wp_reset_query(); ?>

    Latest update.. It’s still not working ;(

  • Alright, if anyone reads this then I got it working.. However, there are problems…

    the code is

    			<?php 
    
    			$list = get_posts(array(
    				'post_type' => 'post',
    				'meta_query' => array(
    								array(
    								'key' => 'cat-to-prod', // name of custom field
    								'value' => '"' . get_the_ID() . '"',
    								'compare' => 'LIKE'
    					)
    				)
    			));
    
    			?>
    			<?php if( $list ): ?>
    				<?php foreach( $list as $list ): ?>
    								<?php
    								$feeds = get_field('_scrape_original_url', $list->ID);
    								$row = array(
    									'field_59dd767200980'	=> $feeds,
    											);
    
    									$i = add_row('field_59dd765c0097f', $row);
    								?>
    
    							<?php endforeach; ?>
    			<?php endif; ?>

    The problem is that it keeps adding the same values over and over to my repeater field in the product…

  • Because you run it on every page load. What you need to do is somehow record which ones you’ve already done and not do them again.

    Also, this can be a problem

    
    <?php foreach( $list as $list ): ?>
    

    these to variable names need to be different

  • John thanks for your valuable input. It’s true that I load them on page load for now, as I’m beta testing it right now.. Most probably, it will be a cron-job done in every 10-15 minutes when the project is up separate from the wordpress or woocommerce loop.

    Right now, I’m planning to add if else function within the row addition.. I’ll parse the full code once it works perfectly 😉

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

The topic ‘post content into repeater fields’ is closed to new replies.