Support

Account

Forum Replies Created

  • Your code looks correct to do what you describe. In what way doesn’t it work?

    What template is running the loop?
    Are you sure the filter code is running? You could try placing a var_dump inside the filter inside the second if check and see if it shows on the page.

  • Thanks for the screencast of the issue – very helpful!

    I noticed in the ACF field settings for the before_photo field, you have the return value set to Image Array. With this setting, more than 1 value is returned meaning WordPress doesn’t know which value to place in the src.

    You can change the return type to Image URL and this should give the expected result.

  • Assuming the update_field() call in your post works as expected (I haven’t looked into this) you should be able to loop the output from WordPress’s get_post_meta() function.

    Something like this:

    
    function update_fields_for_id( $post_id ) {
    	$fields = get_post_meta( $post_id );
    
    	foreach ($fields as $field_name => $field_value) {
    		// Don't process fields with leading underscore
    		// which are marked as private for internal use only
    		if ( 0 !== strpos( $field_name, '_' ) ) {
    			update_field( $field_name, get_field( $field_name, $post_id ), $post_id );
    		}
    	}
    }
    
  • Hi neur,

    Based on what you’ve posted, I think you’re going for something like the code below. I’ve changed a number of things:

    1. The PHP date() function isn’t used in that way. It converts an integer value of seconds into a string formatted date. If you already know the date, you can pass it as a string on its own like ‘2018-01-01’
    2. From this, I’ve moved the year to its own variable for easy updating. The date range can then be built dynamically
    3. You have the keys needed for sorting and the keys needed for filtering jumbled with each other. I’ve updated them to be separate and added comments above each to show what they do
    4. Finally, since the dates are in the format YYYY-MM-DD, we can use the BETWEEN value for compare to pass both dates at once

    With all of that in mind, I think this should work:

    $year = '2018';
    // Build date range from year
    $date_range = array( $year . '-01-01', $year . '-12-31' );
    
    $posts2018_args = array(
        'post_type' => 'events',
        'posts_per_page' => -1,
        // Sort by custom meta_key
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        // Filter by custom meta query
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'event_category',
                'value' => 'Indoor Track and Field',
                'compare' => '=',
            ),
            array(
                'key' => 'event_date',
                'value' => $date_range,
                'compare' => 'BETWEEN',
                'type' => 'DATE',
            ),
        ),
    );
    
    $posts2018 = get_posts( $posts2018_args );
  • Hi neur,

    Based on what you’ve posted, I think you’re going for something like the code below. I’ve changed a number of things:

    1. The PHP date() function isn’t used in that way. It converts an integer value of seconds into a string formatted date. If you already know the date, you can pass it as a string on its own like '2018-01-01'
    2. From this, I’ve moved the year to its own variable for easy updating. The date range can then be built dynamically
    3. You have the keys needed for sorting and the keys needed for filtering jumbled with each other. I’ve updated them to be separate and added comments above each to show what they do
    4. Finally, since the dates are in the format YYYY-MM-DD, we can use the BETWEEN value for compare to pass both dates at once

    With all of that in mind, I think this should work:

    $year = '2018';
    // Build date range from year
    $date_range = array( $year . '-01-01', $year . '-12-31' );
    
    $posts2018_args = array(
        'post_type' => 'events',
        'posts_per_page' => -1,
        // Sort by custom meta_key
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        // Filter by custom meta query
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'event_category',
                'value' => 'Indoor Track and Field',
                'compare' => '=',
            ),
            array(
                'key' => 'event_date',
                'value' => $date_range,
                'compare' => 'BETWEEN',
                'type' => 'DATE',
            ),
        ),
    );
  • Hi neur,

    Based on what you’ve posted, I think you’re going for something like the code below. I’ve changed a number of things:

    1. The PHP date() function isn’t used in that way. It converts an integer value of seconds into a string formatted date. If you already know the date, you can pass it as a string on its own like '2018-01-01'
    2. From this, I’ve moved the year to its own variable for easy updating. The date range can then be built dynamically
    3. You have the keys needed for sorting and the keys needed for filtering jumbled with each other. I’ve updated them to be separate and added comments above each to show what they do
    4. Finally, since the dates are in the format YYYY-MM-DD, we can use the BETWEEN value for compare to pass both dates at once

    With all of that in mind, I think this should work:

    $year = '2018';
    // Build date range from year
    $date_range = array( $year . '-01-01', $year . '-12-31' );
    
    $posts2018_args = array(
        'post_type' => 'events',
        'posts_per_page' => -1,
        // Sort by custom meta_key
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        // Filter by custom meta query
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'event_category',
                'value' => 'Indoor Track and Field',
                'compare' => '=',
            ),
            array(
                'key' => 'event_date',
                'value' => $date_range,
                'compare' => 'BETWEEN',
                'type' => 'DATE',
            ),
        ),
    );
  • I haven’t personally tried filtering a message field, however your syntax looks slightly incorrect.

    See this page: https://www.advancedcustomfields.com/resources/acf-load_value/

    Specifically:

    // acf/load_value/name={$field_name} - filter for a specific value load based on it's field name
    add_filter('acf/load_value/name=my_select', 'my_acf_load_value', 10, 3);
    
    // acf/load_value/key={$field_key} - filter for a specific field based on it's name
    add_filter('acf/load_value/key=field_508a263b40457', 'my_acf_load_value', 10, 3);

    You have name in your code where you are passing a field key.

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