Support

Account

Forum Replies Created

  • I have figured this out. The solution is as below, with the user field set to return “User Array”:

    
    <?php $author_id = get_field('user')['ID']; ?>
    by <a href="<?php echo get_author_posts_url($author_id) ?>" rel="author"><?php echo get_field('user')['display_name']; ?></a>
    

    Credit to @imvrk at this thread for the steer: https://support.advancedcustomfields.com/forums/topic/display-multiple-selected-users-not-working/

  • Yes this does really seem like best practice, so I hope you’ll forgive me being dogged about understanding.

    My understanding is clearly still flawed. from your article and your code above, I arrived at the following:

    add_filter('acf/save_post', 'convert_event_status_to_standard_wp_meta', 20);
    function convert_event_status_to_standard_wp_meta($post_id) {
    
     $meta_key = 'status_wp';
      delete_post_meta($post_id, $meta_key);
      $saved_values = array(); 
      $values = get_field('status', $post_id);
      foreach ($values as $value) {
      if (isset($saved_values[$value])) {
       continue;
      }
      add_post_meta($post_id, $meta_key, $value, false);
      $saved_values[$value] = $value;
     }
    }

    I placed this in functions.php (was that right?), and changed the query to include:

    array(
     'meta_key' => 'status_wp',
     'value' => array('Cancelled', 'Exclude Past'),
     'compare' => '!='
    )

    This gave me the following error:

    Warning: trim() expects parameter 1 to be string, array given in [url]/wp-includes/class-wp-meta-query.php on line 695

    Obviously I’ve not implemented this correctly. I’m guessing either my query is wrong, or I’ve not adapted the repeater example from your article correctly in the foreach section of the code?

    Thanks.

  • That sounds really interesting – great article, and thanks. I’m not advanced enough with PHP to be sure how you would tackle the part that cycles through the repeaters in your example for a checkbox field:

    if (have_rows('colors', $post_id)) {
     while (have_rows('colors', $post_id)) {
      the_row();
      $color = get_sub_field('color');
      if (isset($saved_values[$color])) {
       continue;
      }
      add_post_meta($post_id, $meta_key, $color, false);
      $saved_values[$color] = $color;  
     }
    }

    Presumably we can’t do if(have_rows()) and while(have_rows()) if there are no rows?

    I’m interested in doing this BEFORE the site I’m building gathers a lot of content, and learning something I can use again in future too. Would you mind showing me how you’d do this for a non-repeater?

    Thanks so much.

  • I’m not sure how do check LIKE to exclude something – it would need to be LIKE everything it isn’t, but since multiple values are in play here (it’s a checkbox rather than a radio button) I think I can’t say LIKE a value and assume that means it is necessarily NOT LIKE one of the others.

    That said, I have done this:

    'meta_query' => array(
     'relation' => 'AND',
     array(
      'relation' => 'OR',
      // check to see if end date has been set
      array(
       'key' => 'end_date',
       'compare' => 'BETWEEN',
       'type' => 'NUMERIC',
       'value' => array($date_1, $date_2),
      ),
      // if no end date has been set use start date
      array(
       'key' => 'start_date',
       'compare' => 'BETWEEN',
       'type' => 'NUMERIC',
       'value' => array($date_1, $date_2),
      )
     ),
     array(
      'key' => 'status',
      'value' => '"Cancelled"',
      'compare' => 'NOT LIKE'
     ),
     array(
      'key' => 'status',
      'value' => '"Exclude Past"',
      'compare' => 'NOT LIKE'
     )
    ),

    It seems to be working, although I don’t fully trust it. Now any event I designate as either Cancelled OR Exclude Past drops out of the past events list as I’d expect. Given that the clauses are part of an AND relation I’m not sure I understand why that’s happening?

    Also (and as an aside), I’m noticing that when wrapping ORs inside ANDs like this things work pretty normally, but if I nest queries the other way around the page tends to hang instead of loading. Is that to be expected? Are there rules about which way round these things can be done?

    Again, thanks so much John.

  • I’ve taken what you’ve suggested on board here John – thanks for the input. I understand and can see how it should definitely work. However, the OR relation is behaving like an AND relation, and I can’t work out why.

    Here’s what I have now:

    $past_args = array(
     'post_type' => 'event',
     'meta_query' => array(
     'relation' => 'AND',
     array(
      'relation' => 'OR',
      // check to see if end date has been set
      array(
       'key' => 'end_date',
       'compare' => 'BETWEEN',
       'type' => 'NUMERIC',
       'value' => array($date_1, $date_2),
      ),
      // if no end date has been set use event/start date
      array(
       'key' => 'start_date',
       'compare' => 'BETWEEN',
       'type' => 'NUMERIC',
       'value' => array($date_1, $date_2),
      )
     ),
     array(
      'relation' => 'OR',
      // check event is not cancelled
      array(
       'key' => 'status',
       'value' => '"Cancelled"',
       'compare' => 'NOT LIKE'
      ),
      // check event is not excluded from past events
      array(
       'key' => 'status',
       'value' => '"Exclude Past"',
       'compare' => 'NOT LIKE'
      )
     )
    ),
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'nopaging' => false,
    'posts_per_page' => '9999'
    );

    The OR in the date clause is working fine. In the second set of queries (against the ACF checkbox) though, I am seeing all events with either checkbox value, and only missing those with BOTH values checked. This is what i would expect of an AND relation, no?

    I removed the date clause entirely to try and isolate this but even then I can’t get the query to return events with only one of those two options checked.

    Thanks.

  • I don’t seem to be able to post a reply here. My message is being labelled as a duplicate, although it’s not here.

  • Thanks John,

    I get it – and here’s what I’ve written:

    $past_args = array(
     'post_type' => 'event',
     'meta_query' => array(
     'relation' => 'AND',
     array(
      'relation' => 'OR',
      // check to see if end date has been set
      array(
       'key' => 'end_date',
       'compare' => 'BETWEEN',
       'type' => 'NUMERIC',
       'value' => array($date_1, $date_2),
      ),
      // if no end date has been set use event/start date
      array(
       'key' => 'start_date',
       'compare' => 'BETWEEN',
       'type' => 'NUMERIC',
       'value' => array($date_1, $date_2),
      )
     ),
     array(
      'relation' => 'OR',
      // check event is not cancelled
      array(
       'key' => 'status',
       'value' => '"Cancelled"',
       'compare' => 'NOT LIKE'
      ),
      // check event is not excluded from past events
      array(
       'key' => 'status',
       'value' => '"Exclude Past"',
       'compare' => 'NOT LIKE'
      )
     )
    ),
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'nopaging' => false,
    'posts_per_page' => '4'
    );

    BUT – despite there clearly being an OR relation for the two queries on the status field, it is behaving like an AND relation. The event is only left out of the query if both boxes are checked.

    Any idea why this would be? Might it have something to do with being inside an AND relation? The OR relation on the date clause is working fine. I’ve pulled this right back to individual nested queries to try and isolate the problem but it has really stumped me.

    Thanks.

  • Thanks John,

    I can see how ‘NOT LIKE’ would work if there were only one option to consider. Here I need something like ‘not like exclude past OR not like cancelled’ (there are other checkbox values in this field which I do need to let through).

    Possible values:
    – Cancelled
    – Exclude Past
    – Online
    – No Venue

    Of course it’s also possible that none will be selected. The behaviour I am looking to create is that any post which has either the Cancelled or the Exclude Post (or both) checked is left out of the query, and that everything else gets through.

    So ‘NOT LIKE’ still only excludes/includes based on a single possible checkbox value, correct? How do we do that for more than one of them?

    That’s interesting about the date values. I’ve been using that part of this query for ages and it works fine for me (provided I set the return value of the date field correctly). I’ll switch to NUMERIC and see what happens!

    Thanks.

  • Yup you’re quite right. I found this just before I saw your reply. I called esc_html simply because I was trying everything I could think of. : )

    As always, thank you John!

  • i’m seeing this issue too – almost certainly i’m doing something stupid but i’ve done this now in so many sites and i can’t for the life of me figure out what’s different here.

    here’s the code:

    <?php $images = get_field('project_images');
    if( $images ): ?>
     <div class="car-main-col" id="masonry-wrap" data-columns>    
      <?php foreach( $images as $image ): 
       $content = '<div>';
       $content .= '<a class="gallery_image" href="'. $image['url'] .'">';
       $content .= '<img src="'. $image['sizes']['medium'] .'" alt="'. $image['alt'] .'" />';
       $content .= '</a>';
       $content .= '</div>';
      if ( function_exists('slb_activate') ){
      $content = slb_activate($content);
      }
      echo $content;?>
      <?php endforeach; ?>
     </div>
    <?php endif; ?>

    and the page can be seen here: https://dev-cro.matmartin.co.uk/project/function/

    i’ve tried running it outside the masonry plugin (salvattore.js) and in separate default page templates without the extra functionality. no dice.

    any help would be most welcome.

    thanks.

  • I’m trying to add ACF information to a column in the edit screen for a custom post type (Events) via the functions.php file of my theme. The issue is that the info I’m looking to add is from a post_object field and is returning the post ID rather than the title.

    Here’s the code I’m using:

    function change_columns( $cols ) {
      $cols = array(
        'cb'          => '<input type="checkbox" />',
        'event_date'  => __( 'Event Date', 'trans' ),
        'venue'       => __( 'Event', 'trans' ),
        'city'        => __( 'City', 'trans' ),
        'artist'      => __( 'Artist', 'trans' ),
      );
      return $cols;
    }
    add_filter( "manage_event_posts_columns", "change_columns" );
    
    function custom_columns( $column, $post_id ) {
      switch ( $column ) {
        case "event_date":
          $date = get_field('event_date');          
          echo date("F j, Y (l)", strtotime($date));
          break;
        case "venue":
          echo get_post_meta( $post_id, 'venue_name', true);
          break;
        case "city":
          echo get_post_meta( $post_id, 'city_&_country', true);
          break;
        case "artist":
          echo get_post_meta( $post_id, 'artist', true);
          break;
      }
    }
    add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );

    The result can be seen in the attached screenshot. How can I echo the title of the post_object called in the “artist” column instead of its ID?

  • This is an old thread now, but I found it whilst looking to figure out a similar problem, so for anyone who does the same here’s how I managed it.

    In version 5.3.8 the option to ‘allow null’ was added to the radio button field settings. If you allow this, then make sure the ‘default’ field is empty, nothing is selected on load.

  • Bingo. Thank you so much, once again. I didn’t realise it was possible to reference that event_date field a second time to initiate the sorting. Always learning in this job.

    I really appreciate the help.

  • John, this is great, thank you (as always). It worked exactly as you suggested.

    I am now trying, however, to make sure I can still present them in date order (the other meta query). I’ve been using this to do that in other feeds:

    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'nopaging' => true

    Of course, now there are two meta queries, there are two meta values too, so how can I specify which is to be used for ordering? At the moment it seems a bit confused.

    Here’s the whole thing as it stands currently:

    $byartist_args = (array(
     'numberposts' => -1,
     'post_type' => 'Event',
     'meta_query' => array(
      'relation' => 'AND',
      array(
       'key' => 'artist',
       'value' => get_the_ID(),
       'compare' => '=',
      ),
      array(
       'key' => 'event_date',
       'compare' => 'BETWEEN',
       'type' => 'DATE',
       'value' => array($date_1, $date_2),
      ),
     ),
     'orderby' => 'meta_value_num',
     'order' => 'ASC',
     'nopaging' => true
    ));

    Thank you!

  • I am still struggling with this. After more reading, experimenting, testing etc I have come up with this set of args for the query I am trying to create, but still with no output.

    <?php 
     //Set server timezone to GMT
     date_default_timezone_set('Europe/London'); 
     //Today's date
     $date_1 = date('Ymd', strtotime("now")); 
     //Future date - the arg will look between today's date and this future date to see if the post fall within the 2 dates
     $date_2 = date('Ymd', strtotime("+48 months"));
    ?>
    
    <?php
     $byartist_args = (array(
      'numberposts' => -1,
      'post_type' => 'Event',
      'meta_query' => array(
      'relation' => 'AND',
      array(
       'key' => 'artist',
       'value' => '"' . get_the_ID() . '"',
       'compare' => 'LIKE',
      ),
      array(
       'key' => 'event_date',
       'compare' => 'BETWEEN',
       'type' => 'DATE',
       'value' => array($date_1, $date_2),
       ),
      ),
     ));
    ?>	
    <?php $byartist_query = new WP_Query( $byartist_args ); ?>

    I can only assume that the issue is with this part, as everything else seems to be working okay:

    'value' => '"' . get_the_ID() . '"',

    There must be a way to compare the current post ID with the ID in a post object field and output based on this. Anyone?

  • This has been solved with some help from the ACF team. The issue is the output format on the date picker fields. In order for this to work these must be set to Ymd.

  • thanks @allisonplus, no i didn’t get this fixed as in the end the build took me in a different direction and i didn’t need it for this specific project, but it’s helpful to know you think the code is correct. i’m sure i’ll need to do this at some point in the future so i guess i’ll see whether next time i have the same problem!

  • Thanks John,

    I’m not sure how to reformat the date but this is a secondary issue to the sorting one, and that’s now fixed. Appreciate your help. The complete code works out as:

    // Change the columns for the edit CPT screen
    
    function change_columns( $cols ) {
      $cols = array(
        'cb'          => '<input type="checkbox" />',
        'event_date'  => __( 'Event Date', 'trans' ),
        'venue'       => __( 'Venue', 'trans' ),
        'city'        => __( 'City', 'trans' ),
        'artist'      => __( 'Artist', 'trans' ),
      );
      return $cols;
    }
    add_filter( "manage_event_posts_columns", "change_columns" );
    
    function custom_columns( $column, $post_id ) {
      switch ( $column ) {
        case "event_date":
          echo get_post_meta( $post_id, 'event_date', true);
          break;
    
        case "venue":
          echo get_post_meta( $post_id, 'venue_name', true);
          break;
    
        case "city":
          echo get_post_meta( $post_id, 'city_&_country', true);
          break;
    
        case "artist":
          echo get_post_meta( $post_id, 'artist', true);
          break;
      }
    }
    add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
    
    // Make edit screen columns sortable
    
    add_filter( 'manage_edit-event_sortable_columns', 'my_sortable_event_column' );
    
    function my_sortable_event_column( $columns ) {
        $columns['event_date'] = 'event_date';
        $columns['artist'] = 'artist';
    
        return $columns;
    }
    
    add_action( 'pre_get_posts', 'manage_wp_posts_be_qe_pre_get_posts', 1 );
    function manage_wp_posts_be_qe_pre_get_posts( $query ) {
    
       if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
          switch( $orderby ) {
             case 'event_date':
                $query->set( 'meta_key', 'event_date' );
                $query->set( 'orderby', 'meta_value' );      
                break;
    
            case 'artist':
                $query->set( 'meta_key', 'artist' );
                $query->set( 'orderby', 'meta_value' );      
                break;
          }
       }
    }

    I’ll keep looking for a date solution.

  • thanks john, and apologies for not replying sooner. the client asking for this function changed their mind about needing it so this has become a self-improvement issue for me and thus has slipped in priority.

    thanks for the pointers, i think i see what you mean and although i’ve not had a chance to test anything yet i’ll try and get it working as you suggest.

  • John, this is spot on – thank you. I had managed to get close but not found this exact nest. I’m now off to look at it really carefully and figure out how it’s working.

    Thanks again for your help.

  • thanks john.

    so if i read you correctly i should end up with something that looks a bit like this?

    <h4>Recent Events</h4>
    <?php 
    //Set server timezone to GMT
    date_default_timezone_set('Europe/London'); 
    //Past cutoff date - the arg will look between today's date and this future date to see if the post fall within the 2 dates
    $date_1 = date('Ymd', strtotime("-24 months")); 
    //Today's date 
    $date_2 = date('Ymd', strtotime("now"));
    ?>		
    <?php
    $recent_args = array(
    'post_type'  => 'event',
    'meta_query' => array(
    'relation' => 'AND',
    array(
    'key'     => 'end_date',
    'value'   => $date_2,
    'compare' => '<',
    ),
    array(
    'key'		=> 'end_date',
    'compare' 	=> 'BETWEEN',
    'type' 		=> 'DATE',
    'value' 	=> array($date_1, $date_2),
    ),
    // if no end date has been set use start date
    array(
    'key'       => 'date',
    'compare' 	=> 'BETWEEN',
    'type' 		=> 'DATE',
    'value' 	=> array($date_1, $date_2),
    ),
    ),
    'orderby' 		=> 'meta_value_num',
    'order'        	=> 'DESC',
    'nopaging'     	=> true
    );
    ?>
    <?php 
    // the recent events query
    $recent_query = new WP_Query( $recent_args ); 
    ?>
    <?php if ( $recent_query->have_posts() ) : ?>
    <?php while ( $recent_query->have_posts() ) : $recent_query->the_post(); ?>

    the problem is that the first AND excludes any events in the past which do not have an end date at all (this is likely to be most of them). the above is working fine for events which have both start and end determined now but i need also to include events that only have one date, as in the first feed on the page.

    you can see what i mean here: http://wpa.matmartin.co.uk/events/ – there is a one-day event in the system which should be appearing in that recent list, too.

  • i found it. the problem was simply that it wasn’t enough to leave the taxonomy filter blank for a catch-all of posts when setting up the post object field – as well as specifying post types, it turns out one must also specify at least one taxonomy in the box below that one.

    it’s working fine now, and i must admit i do feel something of a plum.

    thanks john, as ever, for your help with this.

  • thanks john,

    i don’t think it is – i’m seeing the same thing in both admin and editor accounts. how would i check the permissions on the post type? to my knowledge i haven’;t set any restrictions.

  • i thought of this too and checked but it doesn’t change anything. i’ve just double checked on the dev version on my own server too but it’s the same there.

  • thanks john,

    so, i haven’t found any pre_get_posts filters to disable, and i’m not sure what you mean by point 3) above, which suggests to me that i didn’t add anything of that type to the theme either. where/how would i check this?

    i enabled debug mode and the error log but haven’t seen anything. there were one or two things that came up on the front end which i was able to fix but they were unrelated to this. i’m not seeing anything in the back end where the list is returning empty.

    i may not have used the log properly, as it didn’t create a file.

    i’ve gone back to the dev site which is still up and have found tough that i have the same problem there, so it must be related to something i did just before completing this job and taking it live. do you have any idea where else i could look for the cause please?

    thanks.

Viewing 25 posts - 1 through 25 (of 35 total)