Support

Account

Forum Replies Created

  • Hi John, thanks for the reply.

    It returns a field for a post which I can’t find.

    Your question as to why I am doing it is very valid. I am setting a post ID (for a different post to the current one) as a variable and due to a bug in my code, it got set to ‘new_data’ instead of the proper ID. So I wouldn’t have known about it if I hadn’t written bad code

    It is totally avoidable, it is just irritating me as to why it is happening.

    James

  • Just to update as I realised I never finished this – to get past the problem of it returning the url, I added an extra hook for after ACF_Head had saved which wrote out a new form with the new post_id which I can replace the existing one with. That way I can resave the form again without creating another new entry.

    So my final save code looks like this:

    function my_pre_save_post($post_id) {
    	if( $post_id == 'new_data' ) {
    	    $post = array(
    	        'post_status'  => 'publish' ,
    	        'post_type'  => 'saved_data',
    	    );
    	    $post_id = wp_insert_post($post); 
    	}
    	return $post_id;
    }
    
    function my_acf_save_post( $post_id )
    {
    	acf_form(array(
    		'post_id'		=> $post_id,
    		'field_groups' => array(180)
    	));
    	exit();
        
    }
    
    add_filter( 'acf/pre_save_post', 'my_pre_save_post' );
    add_action( 'wp_ajax_save_app_data', 'acf_form_head' );
    add_action( 'wp_ajax_nopriv_save_app_data', 'acf_form_head' );
    add_action( 'acf/save_post', 'my_acf_save_post', 20 );

    If anyone needs any help with this let me know but I also realise this might not be the most efficient way to achieve an AJAX save…

  • I never worked out what it was about my theme which caused the problem, but changing the theme did fix it for me. Which I know isn’t very helpful but might be a good place to start…

  • It’s this one but I take all the functions out of the functions.php file. Looking at it now, it is quite an old theme…

    https://code.google.com/p/wordpress-naked/

  • Hi John

    I can’t thank you enough for your help. I think it must be my theme because I carried out your instructions in the 2015 theme and it worked fine. I am using wordpress naked which I assumed wouldn’t be the problem because it has nothing in it but clearly there is something there which is wrecking it.

    Thanks again – much appreciated!

    James

  • You could iterate over the repeater field adding the values to an array, then use that array to make the sql statement.

    Like:

    <?php
    $my_sql = array();
    // check if the repeater field has rows of data
    if( have_rows('repeater_field_name') ):
    
     	// loop through the rows of data
        while ( have_rows('repeater_field_name') ) : the_row();
    
           //add value to sql array
           array_push($my_sql, get_sub_field('zip_code');
            
    
        endwhile;
    
    else :
    
        // no rows found
    
    endif;
    $sql = "SELECT * FROM table_name WHERE field_name IN (".implode(",",$my_sql).")";
    
    ?>
  • Thanks again John. The only plugins activated are ACF Pro and the ACF repeater field. I haven’t added any other pre_save_post anywhere else (not in my header, page, footer or functions.php) and I only have one acf_form_head() which is at the top of my single-post.php file, just before get_header().

    I guess if no one else is having this problem it must just be something to do with my set up?

    Thanks again for all your help…

  • You could add all the forms as you said, but in the ACF admin screen, you can apply classes to each form field. If you add unique classes each form element in each form group, you could use JS or just CSS to hide and show the different forms.

    Is that the sort of thing you need to do?

  • Hi John

    Thanks for your reply. It’s not that neither way doesn’t work, because both the methods I mentioned and yours all work, it’s that it always creates TWO posts when I submit the form.

    I really can’t work it out. Could it be a WordPress setting because I guess no-one else is having this problem…?

    Thanks again,

    James

  • Or if you want to know the count before running through the array, try something like:

    if( have_rows('questions') ):
       $my_fields = get_field_object('questions');
       $count = (count($my_fields['value']));
     endif;
  • It might be simpler to have a custom taxonomy (which can be hidden in the UI if you prefer) with a hook so when the page is saved it applies the correct terms to the page based on the template chosen.

    I can point you in the right direction on how to do this if you need?

  • OK this is just a thought as I am only just starting to tackle this myself but it seems that if you just add a bit of javascript to prevent the submit button, you can get all the form fields into an object yourself and then post that through ajax the same way you would any other form.

    A brief theoretical code example:

    jQuery(":submit").click(function(event){
        event.preventDefault();
        var form_data = {"action" : "acf/validate_save_post"};
        jQuery("form#post :input").each(function(){
          form_data[jQuery(this).attr("name")] = jQuery(this).val()
        })
         jQuery.post(ajaxurl, form_data) // you will need to get the ajax url for you site
         .done(function(data){
           //you get back something like {"result":1,"message":"Validation successful","errors":0}
         })
      })

    That would validate the form. After that, you need to submit it. To do that, add actions somewhere in your functions.php to point ajax requests to the acf head function:

    add_action( 'wp_ajax_save_my_data', 'acf_form_head' );
    add_action( 'wp_ajax_nopriv_save_my_data', 'acf_form_head' );

    That will point ajax requests to the acf form head function.

    then you can call that using the same js

    form_data.action = "save_my_data";
            jQuery.post(ajaxurl, form_data)
            .done(function(save_data){
              console.log(save_data)
            })
    

    This can go in the return function of the validation ajax call, with an if statement to check the validation came back ok.

    Only problem I have found so far is it returns a url it wants to go to which throws a js error. Working on that now…

  • Hi

    I’m not sure what you code is doing, so forgive me if this is off key but I think you want something like this:

    while( have_rows('repeater_field_name') ): the_row(); 
    
    $attachment_id = get_sub_field('your image field');											$imageS = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
    $imageF = wp_get_attachment_image_src( $attachment_id, 'full' );
    ?>
    <a href="<?=imageF[0];?>"><img src="<?=imageS[0];?>"></a>
    <?php
    endwhile;

    James

  • You could use a repeater field instead of a gallery field?

    In the repeater, have one image field and one text field which you use to specify the caption.

    James

  • Hi jacobdubail

    I haven’t posted it anywhere but would be happy to share. However, I have since discovered that rather than using the relationship field, which involved using the complicated ajax hack, using the post field, possibly inside a repeater if you want to choose multiple is MUCH easier and can be done with a replica of the post field.Would you be more interested in that?

    The only problem with it over the relationship picker is post types are created at run time. Therefore, if you have custom post types you want to pick from, you will need to create a cache of the list of post types. I have added a hook to my save post to create a cache which sits in my plugin folder and would be very happy to help you do the same if you don’t know how.

    James

  • hello!

    Just incase anyone else was interested, I thought I would share where I got to.

    The problem wasn’t with the query_posts as such, although I can still only get it to work with the switch to blog low down just before the query.

    The problem was that the function wasn’t getting called in the first place. I had thought that changing the add_actions at the top of the file so that any wp ajax action going to the acf query post went to my query post function, it would work.

    What I have actually ended up doing is writing some js in my plugin which watches the wp ajax call and if it is from one of my fields, it redirects it to my query post function.

    I can’t imagine anyone would want to replicate this but if someone does I would be happy to share. I haven’t quite worked out how to pull the list of post types in yet though.

    I think it is great you answer these questions in the first place and I am a big fan of ACF and all the things yo’ve managed to do with it!

  • Hi elliot

    Thanks very much for replying. I had it in later than that so have moved it up to line 133 but I’m still only getting the current blog. The relationship picker works fine apart from picking up the wrong blog…

    Thanks again!

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