Support

Account

Home Forums Search Search Results for 'q'

Search Results for 'q'

reply

  • I have no idea if this will help in your situation, but I solved mine by using $wpdb->get_results. In my particular situation, I had a repeater of “Questions”. Each question had a text field “Question”, and a dropdown which is supposed to display the other items in the repeater.

    This code pulls the “question” field for each “questions” on the current post, and since it’s done directly through the $wpdb query it doesn’t trigger any of the ACF actions or filters, avoiding the infinite loop.

    
    function loadQuestions($field) {
        global $wpdb;
        $field['choices'] = array( 0 => 'None' );
            
        $choices = $wpdb->get_results($wpdb->prepare(
            "SELECT meta_value FROM wp_postmeta WHERE post_id = %d AND meta_key RLIKE %s",
            get_the_id(),
            sprintf('^%s_[0-9]*_%s$', 'questions', 'question')
        ), ARRAY_A);
    
        for ($i = 0; $i < count($choices); $i++) {
            $field['choices'][] = $choices[$i]['meta_value'];
        }
    
        return $field;
    }
    

    This isn’t a perfect solution — my number one issue with it is that it only pulls values that have already been saved in the database, so in my situation, I have to save all the questions before I can set any of the dropdowns. I’m 90% sure this could be solved with a little javascript, but I haven’t had the time to investigate further, and it’s not important enough on my end to warrant the time.

  • Found this thread in the forum, with a similar request and a dead link to a possible solution 🙁

    http://support.advancedcustomfields.com/forums/topic/add-unique-name-field-for-flexible-content-field/

  • Your code looks correct, however, query_posts is used to modify the main query of the post/page you’re on. You’ll probably want to use get_posts instead. The rest remains the same:

    
    $args = array(
      'post_type'     => 'post',
      'cat'      => 399,
      'posts_per_page'    => -1,
      'meta_query'        => array(
        array(
          'key' => 'organization_type',
          'value' => 'socialbusiness',
        )
      )
    );
    ?>
    
    <?php
    $social_businesses = get_posts( $args );
    
    if ( $social_businesses ): ?>
      <?php foreach ( $social_businesses as $sb ): ?>
        <a href="<?php echo get_permalink( $sb->ID ); ?>"><?php echo get_the_title( $sb->ID ); ?></a><br>    
      <?php endforeach; ?>
    <?php endif; ?>
    
  • Assuming the $cats is receiving values it seems like you’ll want to update the code to:

    
    <?php
    $cats = get_field('rel_posts_cat');
    $my_query = new WP_Query(array(
    	'showposts' => '10',
    	'category__and' => $cats, 
    ));
    while ( $my_query->have_posts() ) : $my_query->the_post();
    ?>
    

    The change is on the 'category__and' parameter, since $cats should be returning an array assuming you’ve defined the Return Value to Term ID

  • Ah, that makes sense. So this is rather a reverse relationship query. In that case your friend was correct, but it can be done in a single query:

    
    <?php
    $related_posts = get_posts(array(
      'post_type' 	  => 'post',
      'numposts'	  => -1,
      'post_status'	  => 'publish',
      'orderby'	  => 'title',
      'order'         => 'ASC',
      'meta_query' 	  => array(
        array(
          'key'     => 'related_products',
          'value'	=> '"' . get_the_ID() . '"',
          'compare' => 'LIKE'
        ),
      )
    ));
    ?>
    
    <?php if ( $related_posts ): ?>
      <ul class="medium-block-grid-5 blog-posts">
        <?php foreach( $related_posts as $rp ): ?>
          <li class="post">
            <a href="<?php echo get_permalink( $rp->ID ); ?>" title="<?php the_title_attribute( array( 'post' => $rp->ID ) ); ?>" ><span class="featured-title"><h3><?php echo get_the_title( $rp->ID ); ?></h3></span></a>
            
            <?php if ( has_post_thumbnail( $rp->ID ) ) : ?>
    	  <?php echo get_the_post_thumbnail( $rp->ID, 'regular-posts' ); ?>
    	<?php else : ?>
    	  <img src="<?php echo get_template_directory_uri(); ?>/img/no-image.png" alt="" />
    	<?php endif; ?>
          </li>
        <?php endforeach; ?>
      </ul>
    <?php endif; ?>
    
  • 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…

  • Right now I’ve got the following working:

    1. Read all field groups using acf_local()->get_field_groups().
    2. Filter out all that are not in options pages as per the location matching rules.
    3. Get fields for all groups that are matched using acf_get_local_fields( $fieldgroup_key ).
    4. Read group keys and field keys.
    5. Output HTML based on group and fields as in foreach ( $groups as $fields ) : foreach ( $fields as $field_key => $field_data ) : printf( ... ); endforeach; endforeach;
    6. Said outputted fields are sent using standard HTML form element to the same page template (action="").
    7. Validation, referers, nonces, etc.
    8. Use the POST data to read field keys and update the database values from sanitized $_POST using update_field( $field_key, $value, 'options' ).

    Currently this works fine for non-JS infused fields, such as selects, text fields and so on. I’m working on getting the color picker working, which in the end should use a hidden text field anyway.

    Is there a more correct way to do this or should I wrap this into a plugin of sorts?

  • Brian,

    I guess this is out side the scope of ACF. However, one strategy (that might work) could be:

    1. Hide your images with css:
    #gallery-1 { display:none;}

    2. Create a button “Photos”

    3. Add a jQuery click event listener to the button that performs/fakes a “Click” on the first image – to start the Simple Lightbox:

    jQuery(document).ready(function($) {
    	// cache variables -Id #btn-photos is generic - chang with your own actual ID or Class
    	var btnPhotos = $("#btn-photos"),
    		galleryItems = $("#gallery-1").find(".gallery-item a");
    
    	$("#btn-photos").click(function(event) {
    		// something like this MIGHT work..
    		galleryItems.first().click();
    	});
    });

    Might be better ways, but this is first that came to mind.

    Regards!

  • Certainly. That proposed solution does sound quite a bit more complex than necessary (essentially duplicating what ACF should be doing currently).

    If you do:

    
    $products = get_field('related_products');
    print_r($products);
    

    On a post you know to contain related products what do you get back?

  • Hi ractoon!
    Thanks for responding to my issue. Thanks – I actually tried it that way as well, but it doesn’t yield any results, even though I have the product selected within the relationship block on the admin post page. And to confirm, yes, I’m using post object and the Relationship field type.

    I had a developer friend take a look and although he’s not able to help out with this at the moment, he thinks this is what needs to happen: It has to find the posts of the custom field, then find the id’s from those posts by querying the db. Then, it has to return an array of those fields to the single product template, and based on that array of Id’s, set up a WP Query for those posts.

    So this seems more complicated than I originally anticipated it being. If you have any further thoughts beyond this, I’m all ears. Thanks again for your willingness to respond to my issue! Much appreciated.

  • So this is assuming you’re using the Relationship field type with the return format set to Post Object:

    
    <ul class="medium-block-grid-5 blog-posts">	
    <?php if ( $products = get_field('related_products') ): ?>
      <?php foreach( $products as $p ): ?>
        <li class="post">
          <a href="<?php the_permalink( $p->ID ); ?>" title="<?php the_title_attribute( array( 'post' => $p->ID ) ); ?>" ><span class="featured-title"><h3><?php echo get_the_title( $p->ID ); ?></h3></span></a>
          
          <?php if ( has_post_thumbnail( $p->ID ) ) : ?>
            <?php echo get_post_thumbnail( $p->ID, 'regular-posts' ); ?>
          <?php else : ?>
            <img src="<?php echo get_template_directory_uri(); ?>/img/no-image.png" alt="" />
          <?php endif; ?>
        </li>
      <?php endforeach; ?>
    <?php endif; ?>
    </ul>
    

    Summary is that you don’t need to put this inside a WordPress loop, you can just iterate over the related posts and call the desired functions on that specific post (in this cast product) ID.

  • This can be done just through the stylesheet. So say for example you have the following in your archive file:

    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <div class="entry-content">
         Content outputting here... 
      </div>
    <?php endwhile; ?>
    <?php endif; ?>
    

    I believe each element should be wrapped in something already, like a <div> or <article> or similar. You’ll just target that with your CSS and make it two-columns. So for the example snippet above you’d probably have some CSS like:

    
    .archive-employees .entry-content {
      float: left;
      width: 50%;
    }
    

    That .archive-employees bit is whatever class is currently on the <body> element on your archive page. Might need some tweaking but that’s the general idea.

  • I got the same issue;
    used following workaround:
    temporarily disabling PostTypesOrder, selecting the page in acf, saving and reactivating PTO;
    if you take a look at the custom field again the selected page makes no sense, still works though;

    after reading some feedback in the PTO-forum I used the following fix which is quite acceptable for the moment:

    Go to Settings->Post Types Order and uncheck the “Administrator Sortierung”, guess it’s named something like “administrator sorting” in english backends;

  • Yep, that definitely worked (both of them)!

    Last question then that I’ll ask before closing this out is how I’d make the archive page be two columns, such that it would generate/display content like so:

    employee1 employee2

    employee3 employee4

    I started messing around with it to try to get that but it seems to just list them one after the other in a single column or, if I try to code in a second column, it just repeats the data in that one.

  • Ah, so get_field does not output the value, to do that you’ll need to either echo it or use the_field instead:

    
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php echo get_field('employee_title'); ?>
    

    or

    
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_field('employee_title'); ?>
    

    For doing conditional fields you’ll just need to wrap it in a conditional like:

    
    <?php if ( $linkedin = get_field( 'linkedin' ) ): ?>
    <a href="<?php echo $linkedin; ?>">View LinkedIn</a>
    <?php endif; ?>
    
  • Thanks, that worked for getting the archive to display. I played around with it and got the single employee page to work just fine. However, the overall page that lists all employees, it’s not bad, in that it’s listing the employees one after the other on their own line, single-column. I can click their name to go their single page, and that one seems fine if i just style it (the get_field query is working just fine) but, for the overall employees list page, I’d like its presentation to be two-column, with each employee’s title under their name (title not linked, just their name is linked). I tried this with this syntax:

    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php get_field('employee_title'); ?>

    The archive page is ignoring the get_field and still just displaying only the permalink. Do you know what’s going on there? I figure if I can get the title to display under an employee name, well then I can style it to be two-column thereafter. I’ve attached the archive file for reference.

    Thanks again for your efforts thus far.

    EDIT: Also, while I’m waiting for a reply, I’m noticing some of the employees I’m adding here don’t have a URL for LinkedIn. On their single pages, since it’s a template, it’s still generating a hyperlink, but it’s not pointing to anywhere. Is there a syntax I can toss into the single page archive that says something like “if (field) is blank, don’t display this section?”

  • I looks like when we use $posts = get_field(‘relationship_field’);

    and then use foreach ($posts as $post):

    This causes issues with the $posts variable. I know i was not able to reset it.

    So, i saw this thread on your board.

    http://support.advancedcustomfields.com/forums/topic/displaying-a-relationship-field-on-a-custom-post-type-taxonomy-page/

    And i modified my code to match what her solution ended up being:

    And now i have this:

    <?php
    $post_id = 19;
    
    $headerqry = array( 
    	'p' => $post_id,
    	'post_type' => 'any',
    	'posts_per_page' => 1,
    	'status' => 'published'
    );
    
    $loop = new WP_Query( $headerqry );
    
    if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
    
    $promo_posts = (array) get_field('header_promo_banner');
    
    if( $promo_posts ): ?>
        <?php foreach( $promo_posts as $promo_post): // variable must be called $post (IMPORTANT) ?>
            <?php 
    					$attachment_id = get_field('promo_image', $promo_post->ID);
    					$size = "promo-header";
    					$image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
    				
    				if( $image_attributes ) {
    				?>
            	<a href="<?php echo the_field('promo_url', $promo_post->ID); ?>" target="_blank"><img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"></a>
        <?php } endforeach; ?>
    <?php endif; ?>
    <?php endwhile; endif; wp_reset_query(); ?>

    And this is letting single.php using standard wordpress loop run. So, I would say that there is definitely a problem with the Relationship field when you use the $posts variable.

    Can you please look into this?

  • Actually that did not solve the problem. It seems anytime i put a relationship field in the header, page, sidebar, anywhere – anytime there is a wordpress default loop after that, that loop won’t run.

    If i comment out this code in my header-news.php file, then the rest of the page will run just fine.

    I have tried variations of this code too, but it doesnt seem to work.

    Maybe if you can take a look you can spot something wrong with it.

    <?php
    $post_id = 19;
    
    $headerqry = array( 
    	'p' => $post_id,
    	'post_type' => 'any',
    	'posts_per_page' => 1,
    	'status' => 'published'
    );
    
    $loop = new WP_Query( $headerqry );
    
    if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
    
    $posts = get_field('header_promo_banner');
    
    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <?php 
    					$attachment_id = get_field('promo_image');
    					$size = "promo-header";
    					$image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
    				
    				if( $image_attributes ) {
    				?>
            	<a href="<?php echo the_field('promo_url'); ?>" target="_blank"><img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"></a>
        <?php } endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    <?php endwhile; endif; wp_reset_query(); ?>

    On most pages i am using global $post; $post_id = $post->ID; to get the ID of the current page outside of the loop.

    I was hoping that wp_reset_query would reset the loop and let the items after this query run, but it doesnt seem to work.

    Then the code in my single-10.php file is pretty basic:

    while ( have_posts() ) : the_post(); ?>
    
    	<div class="main-photo-wrapper">
      	<img src="<?php echo the_field('header_photo'); ?>" style="width:100%; height:auto;" alt="<?php echo the_field('header_alt_text'); ?>"/>
    	</div>
      
      <div class="title-bar">
      	<h2><?php the_title(); ?></h2>
      </div>
      
      <div class="article-content">
      <?php the_content(); ?>
      </div>
      
    <?php endwhile;  // end of the loop. ?>
  • I’ve found that Cocoa JSON Editor work’s well enough. (PS: I work on a Mac.) Nonetheless, this editor – as well as the other JSON editor’s I’ve played with – lack certain features that are key to creating or iterating on ACF field groups. For example, it would be extremely useful to be able to automatically generate unique IDs, save snippets (or somehow be able to automatically insert a predefined field or set of fields), among others.

    I think it would be relatively easy to build a web interface that solves most of these issues, but alack! I have literally zero free time at the moment…

  • Please help with this request. Hello?

  • Just added a Filter for that custom field
    `
    add_filter(‘acf/fields/user/query/name=customfieldname’, ‘acf_refernt_field_filter’, 10, 3);
    function acf_refernt_field_filter( $args, $field, $post )
    {
    // modify get users args of that field
    $args[‘role’] = ‘my-custom-user-role’;

    return $args;
    }
    `

  • Just added a Filter for that custom field
    `
    add_filter(‘acf/fields/user/query/name=customfieldname’, ‘acf_refernt_field_filter’, 10, 3);
    function acf_refernt_field_filter( $args, $field, $post )
    {
    // modify get users args of that field
    $args[‘role’] = ‘my-custom-user-role’;

    return $args;
    }
    `

  • Many Many thanks thisislawatts!
    your hack worked as a charm.

    I’m a big fan of ACF, but this is quite disappointing. I did not have any asnwer from official support, not even a notice about the missing db upgrade.

  • I know this post is over a year old now, but I recently had a similar problem to kylerumble and wanted to provide a solution on the off-chance that someone else can use it.

    The problem: A company takes part in several events, some of which span multiple days, and wants to highlight them across two sections on their website: Upcoming and Past. The Past section shows all events from the previous year up until (the relative) yesterday with the exception of multi-day events. If an event spans multiple days, it continues to be shown in the Upcoming section until the current date is after the last day of the event.

    Like kylerumble, I didn’t want the editors to have to fill in the date twice if the event only lasts a single day.

    The solution:

    I made three ACF fields – “Multi-day Event” (True / False field), “Start Date,” and “End Date” (both Date Picker fields) – and set a conditional around the Start Date field so it only displays if the “Multi-day Event” field is checked.

    Using Jonathan’s last function as a starting point, I used the save_post action to update the field when necessary:

    // run AFTER ACF saves the $_POST['fields'] data to database
    add_action('acf/save_post', 'single_day_event', 20);
    
    function single_day_event( $post_id ) {
        // Ensure we have ACF fields on the post.
        if( empty($_POST['acf']) ) {
            return;
        }
        
        // Set up our variables with the respective ACF field keys
        $multi_day_event = $_POST['acf']['field_123'];
        $start_date_field = $_POST['acf']['field_456'];
        $end_date_field = $_POST['acf']['field_789'];
    
        /* Update the Start Date field value with the End Date field value if any of the following are true:
        1. The Start Date field is empty
        2a. Multi-day Event is empty and the Start Date doesn't equal the End Date
        2b. Multi-day Event's value is 0 and the Start Date doesn't equal the End Date (This is in case an event is ever set to Multi-day and then later changed to a single day)
        */
        if( ( $start_date_field == '' ) || ( ( $multi_day_event == '' || $multi_day_event == 0 ) && ( $start_date_field != $end_date_field ) ) ){
    		update_field('field_456', $end_date_field, $post_id);
    	}
    	
    }

    Cheers!

  • Ok i think i figured things out.

    function last_paragraph( $content ){
      if ( is_page( 222 ) ) {
        return preg_replace('/[\s\S]*\K(<p>)/', '<p class="mytrail">', $content, 1);
      } 
    }
    add_filter('acf_the_content', 'last_paragraph');

    I’ve set the function only to a certain page and on that page it targets only acf fields ( which are in my case only text fields, text areas and wysiwyg fields). Text fields and text areas don’t have any wrapping p tags therefor only wysiwyg fields are targeted. And with that small function it is possible that the user is able to add more paragraphs in each wysiwyg field and still the class is set on the last paragraph of each wysiwyg field.

Viewing 25 results - 17,351 through 17,375 (of 21,345 total)