Support

Account

Home Forums Search Search Results for '%s'

Search Results for '%s'

reply

  • I figured it out. Here is my code…

    <section>
        <?php
    $args = array(
      'orderby' => 'name',
      'order' => 'ASC'
      );
    $categories = get_categories('child_of=39', $args);
      foreach($categories as $category) { ?>
        <div class="casino-games">
          <?php $image_url=get_field('featured_category_image', $category); ?>
          <a href="<?php get_category_link( $category->term_id ) ?>" title="<?php sprintf( __( "View all posts in %s" ), $category->name ) ?>" />
          <h3><?php echo $category->name ?></h3>
          <img src="<?php echo $image_url ?>" /> </a> </div>
        <?php } ?>
        <div class="clear"></div>
      </section>

    it was this part that did the trick for me…
    <?php $image_url=get_field('featured_category_image', $category); ?>

  • Hi guys.

    Thanks for all the info in this thread. I’ve done some testing and believe I have found the problem and fixed it!

    Please find bellow a re-written get_field_objects function which will fix the issue. Please copy / paste this over the origional function found in the api/api-template.php file.

    
    /*
    *  get_field_objects()
    *
    *  This function will return an array containing all the custom field objects for a specific post_id.
    *  The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the fields / values.
    *
    *  @type	function
    *  @since	3.6
    *  @date	29/01/13
    *
    *  @param	$post_id (mixed) the post_id of which the value is saved against
    *  @param	$format_value (boolean) whether or not to format the field value
    *  @param	$load_value (boolean) whether or not to load the field value
    *  @return	(array)	associative array where field name => field
    */
    
    function get_field_objects( $post_id = false, $format_value = true, $load_value = true ) {
    	
    	// global
    	global $wpdb;
    	
    	
    	// filter post_id
    	$post_id = acf_get_valid_post_id( $post_id );
    
    	// vars
    	$meta = array();
    	$fields = array();
    	
    				
    	// get field_names
    	if( is_numeric($post_id) ) {
    		
    		$meta = get_post_meta( $post_id );
    	
    	} elseif( strpos($post_id, 'user_') !== false ) {
    		
    		$user_id = (int) str_replace('user_', '', $post_id);
    		
    		$meta = get_user_meta( $user_id );
    		
    	} elseif( strpos($post_id, 'comment_') !== false ) {
    		
    		$comment_id = (int) str_replace('comment_', '', $post_id);
    		
    		$meta = get_comment_meta( $comment_id );
    		
    	} else {
    		
    		$rows = $wpdb->get_results($wpdb->prepare(
    			"SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s",
    			$post_id . '_%' ,
    			'_' . $post_id . '_%' 
    		), ARRAY_A);
    		
    		if( !empty($rows) ) {
    			
    			foreach( $rows as $row ) {
    				
    				$meta[ $row['option_name'] ][] = $row['option_value'];
    				
    			}
    			
    		}
    		
    	}
    	
    	
    	// populate vars
    	if( !empty($meta) ) {
    		
    		foreach( $meta as $k => $v ) {
    			
    			// Hopefuly improve efficiency: bail early if $k does start with an '_'
    			if( $k[0] === '_' ) {
    				
    				continue;
    				
    			}
    			
    			
    			// does a field key exist for this value?
    			if( !array_key_exists("_{$k}", $meta) ) {
    				
    				continue;
    				
    			}
    			
    			
    			// get field
    			$field_key = $meta["_{$k}"][0];
    			$field = acf_get_field( $field_key );
    			
    			
    			// bail early if not a parent field
    			if( empty($field) || acf_is_sub_field($field) ) {
    				
    				continue;
    				
    			}
    			
    			
    			// load value
    			if( $load_value ) {
    			
    				$field['value'] = acf_get_value( $post_id, $field );
    				
    				// format value
    				if( $format_value ) {
    					
    					// get value for field
    					$field['value'] = acf_format_value( $field['value'], $post_id, $field );
    					
    				}
    				
    			}
    			
    						
    			// append to $value
    			$fields[ $field['name'] ] = $field;
    		}
    		
     	}
     	
     	 	
    	// no value
    	if( empty($fields) ) {
    	
    		return false;
    	
    	}
    	
    	
    	// return
    	return $fields;
    }
    

    Please let me know how you go after updating the file.

  • Hi @JeroenReumkens

    Thanks for the bug report. I have added this back in (was a mistake to leave it out) and you will find the fix included in the next update.

    For now, please modify the pro/fields/flexible-content.php file on line 210 to:

    
    // no value message
    		$no_value_message = __('Click the "%s" button below to start creating your layout','acf');
    		$no_value_message = apply_filters('acf/fields/flexible_content/no_value_message', $no_value_message, $field);
    
    ?>
    <div <?php acf_esc_attr_e(array( 'class' => 'acf-flexible-content', 'data-min' => $field['min'], 'data-max'	=> $field['max'] )); ?>>
    	
    	<div class="no-value-message" <?php if( $field['value'] ){ echo 'style="display:none;"'; } ?>>
    		<?php printf( $no_value_message, $field['button_label'] ); ?>
    	</div>
    

    Thanks
    E

  • Try changing
    echo the_field('contact_email');

    to

    printf('<a href="mailto:%s" title="Email Me" rel="nofollow">%1$s</a>', get_field("contact_email"));

  • Missed the ACF Pro update. So I see its not using postmeta anymore…

    Here’s the updated code for anyone thats looking.

    function update_message_field($field_key='', $message='')
    {
    	global $wpdb;
    
    	$table = $wpdb->prefix.'posts';
    	$field = $wpdb->get_results("SELECT * FROM $table WHERE post_name = '$field_key' AND post_type='acf-field'");
    	if($field)
    	{
    		$meta = unserialize($field[0]->post_content);
    		$meta['message'] = $message;
    		$wpdb->update(
    			$table,
    			array(
    				'post_content'=>serialize($meta)
    			),
    			array('post_name'=>$field_key, 'post_type'=>'acf-field'),
    			array('%s')
    		);
    	}
    }
  • I’ve cobbled together a quick solution to do this.

    Anyone have a better idea let me know!

    update_message_field('field_53f8663f25abd', '<b>My message</b>');
    
    function update_message_field($field_key='', $message='')
    {
    	global $wpdb;
    
    	$table = $wpdb->prefix.'postmeta';
    	$field = $wpdb->get_results("SELECT * FROM $table WHERE meta_key = '$field_key'");
    	if($field)
    	{
    		$meta = unserialize($field[0]->meta_value);
    		$meta['message'] = $message;
    		$wpdb->update(
    			$table,
    			array(
    				'meta_value'=>serialize($meta)
    			),
    			array('meta_key'=>$field_key),
    			array('%s')
    		);
    	}
    }
  • Found it. I was using this code in my functions.php:

    		$showID = $wpdb->get_var( $wpdb->prepare( "
    			SELECT ID 
    			FROM $wpdb->posts 
    			WHERE post_name = %s 
    			AND post_type= %s", 'show', 'page' ) );
    			
    		if ( !$showID ) {
    			$showID = wp_insert_post( array(
    				'post_name' => 'show',
    				'post_title' => 'Show',
    				'post_type' => 'page',
    				'post_status' => 'publish'
    				));
    		}
    		wp_update_post( array(
    			'ID'           => $showID,
    			'post_status' => 'private'
    		) );
    

    Turns out the wp_update_post with just the ID and post_status makes this issue alive. Even though it has the correct ID to the page.

  • I was able to manage it somehow. This is the sql query. It retrieve all the published posts of post_type “attivita”, according to the language (i’m using polylang) and to the “dayname” key_value.

    $language = pll_current_language();
    $rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT * 
                FROM wp_posts
    			INNER JOIN wp_postmeta m1
    				ON (wp_posts.ID = m1.post_id)
    			INNER JOIN wp_term_relationships wtr 
    				ON (wp_posts.ID = wtr.object_id)
    			INNER JOIN wp_term_taxonomy wtt 
    				ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id)
    			INNER JOIN wp_terms wt 
    				ON (wt.term_id = wtt.term_id)
    			WHERE 
    				wp_posts.post_type = 'attivita'
    			AND wp_posts.post_status = 'publish'
    			AND wtt.taxonomy = 'language' 
    			AND wt.slug = %s
    			AND (m1.meta_key LIKE %s AND meta_value = %s)
                ",
    			$language,
    			'orario_%_giorno', // meta_name: $ParentName_$RowNumber_$ChildName
                $dayname // meta_value: 'type_3' for example
            ));

    Now i’m trying to display posts according to the starting time, stored as a subfield (attivita means activity, it’s a sort of calendar). all the activities take place in the morning (starting 9am) or in the afternoon (starting 3pm).

    So the code above is inserted in a while loop that counts day-by-day at each loop, and inside the while loop i have two foreach that displays the activities within each single day, for the morning and the afternoon respectively.

  • I don’t see any difference between date(‘Ymd’) and current_time(‘Ymd’); They both output todays date.

    It seems to do what I wanted this to do was even more involved. The tricky part was when an event spanned multiple months. Say an Event would start in May and end in July. The event would not show up in June because it was out of the scope of the range of the event.

    So, a very smart fellow by the name of “keesiemeijer” over at wordpress.org helped me get the query correct.

    Here is the solution:

    Add this to your functions.php file – modify as needed:

    function get_meta_sql_date( $pieces, $queries ) {
        global $wpdb;
    
        // get start and end date from query
        foreach ( $queries as $q ) {
    
            if ( !isset( $q['key'] ) ) {
                return $pieces;
            }
    
            if ( 'event_start_date' === $q['key'] ) {
                $start_date = isset( $q['value'] ) ?  $q['value'] : '';
            }
            if ( 'event_end_date' === $q['key'] ) {
                $end_date = isset( $q['value'] ) ?  $q['value'] : '';
            }
        }
    
        if ( ( '' === $start_date ) || ( '' === $end_date ) ) {
            return $pieces;
        }
    
        $query = "";
    
        // after start date AND before end date
        $_query = " AND (
            ( $wpdb->postmeta.meta_key = 'event_start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s) )
            AND ( mt1.meta_key = 'event_end_date' AND ( CAST(mt1.meta_value AS DATE) <= %s) )
        )";
        $query .= $wpdb->prepare( $_query, $start_date, $end_date );
    
        // OR before start date AND after end end date
        $_query = " OR (
            ( $wpdb->postmeta.meta_key = 'event_start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) )
            AND ( mt1.meta_key = 'event_end_date' AND ( CAST(mt1.meta_value AS DATE) >= %s) )
        )";
        $query .= $wpdb->prepare( $_query, $start_date, $end_date );
    
        // OR before start date AND (before end date AND end date after start date)
        $_query = " OR (
            ( $wpdb->postmeta.meta_key = 'event_start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) )
            AND ( mt1.meta_key = 'event_end_date'
                AND ( CAST(mt1.meta_value AS DATE) <= %s )
                AND ( CAST(mt1.meta_value AS DATE) >= %s )
            )
        )";
        $query .= $wpdb->prepare( $_query, $start_date, $end_date, $start_date );
    
        // OR after end date AND (after start date AND start date before end date) )
        $_query = "OR (
            ( mt1.meta_key = 'event_end_date' AND ( CAST(mt1.meta_value AS DATE) >= %s ) )
            AND ( $wpdb->postmeta.meta_key = 'event_start_date'
                AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s )
                AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s )
            )
        )";
        $query .= $wpdb->prepare( $_query, $end_date, $start_date, $end_date );
    
        $pieces['where'] = $query;
    
        return $pieces;
    }

    Then add this to your page where you do your query.

    <?php
    add_filter( 'get_meta_sql', 'get_meta_sql_date', 10, 2 );
    $all_events = array (
        'post_type' => 'events',
        'posts_per_page' => 50,
    
        // creates the meta sql join and where clauses
        // which will be filtered in functions.php
        // must be two meta_query arrays
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'       => 'event_start_date',
                'compare'   => '>=',
                'value'     => '20140601',
                'type'      => 'DATE'
            ),
            array(
                'key'       => 'event_end_date',
                'compare'   => '<=',
                'value'     => '20140630',
                'type'      => 'DATE'
            )
        ),
    );
    
    $date_query = new WP_Query( $all_events );
    remove_filter( 'get_meta_sql', 'get_meta_sql_date', 10, 2 );
    ?>

    So, holly molly was this involved. I would recommend that they add a similar solution to the documentation on the ACF website.

    This worked for me.

  • I did it from within the loop so, while not totally straightforward, allowed me to use get_field('participants'), and then foreach( $participants as $participant ) to access each user/participant associated with a given post as an array. From there I could get the user details.

    However, this is not going to work outside of the loop. You could try http://codex.wordpress.org/Class_Reference/wpdb to get specific meta_key values

    I think the following would return an array of all users (where the meta_key is ‘participants’). As users information is itself stored as an serialized array, you’d then need to dig into this.

    $metakey = 'participants';
    $participants = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );

    I’m pretty new to PHP so don’t bet your house on anything here, although I’ve successfully tried all the above. Best of luck

  • Hi Elliot,

    Thanks for your response.

    I’ve tried it like this ;

    <?php echo get_sub_field('adres_lid', $row->post_id); ?>

    But it returns empty. Or isn’t it possible to show a subfield like that?

    This is the whole code now

    <?php
         
                            // get all rows from the postmeta table where the sub_field (type) equals 'type_3'
                            // - http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
                                $rows = $wpdb->get_results($wpdb->prepare( 
                                    "
                                    SELECT * 
                                    FROM wp_postmeta
                                    WHERE meta_key LIKE %s
                                        AND meta_value = %s
                                    ",
                                    'leden_details_%_plaats_lid', // meta_name: $ParentName_$RowNumber_$ChildName
                                    'gytsjerk' // meta_value: 'type_3' for example
                                ));
                         
                            // loop through the results
                            if( $rows )
                            {
                                foreach( $rows as $row )
                                {
                                   
                         
                            ?>
    
                                <li class="leden-details"><?php echo get_the_title( $row->post_id ); ?></li>
                                <li class="leden-show"><?php echo get_sub_field('adres_lid', $row->post_id); ?></li>
    
                        <?php
                     
                            }
                        }
                     
                        ?>

    Thanks

  • I’ve implemented this – be interested to get your opinion or is there’s a better way of doing it

    This function returns an associative array given a Field Group title. The array keys are the field name slugs, and the array values are the field keys.

    function get_acf_field_keys($field_group_title) {
    
            global $wpdb;
    
            // get the post id for this field group
            $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = '%s' AND post_type = 'acf'", $field_group_title );
            $field_group_id = $wpdb->get_var($sql);
    
            // now get all postmeta items (the fields in the fieldgroup) for that postid
            $sql = $wpdb->prepare(" SELECT meta_value from $wpdb->postmeta
                                    WHERE post_id = '%d' AND meta_key LIKE 'field%%'",$field_group_id);
            $acf_postmeta_fields = $wpdb->get_results($sql);
    
            $fields = array();
            foreach ($acf_postmeta_fields as $acf_postmeta_field) {
                $unserialized = unserialize($acf_postmeta_field->meta_value);
                $fields[$unserialized['name']] =  $unserialized['key'];
            }
            return $fields;
        }
  • Hi Elliot,

    There is no error in the source except

    <script type='text/javascript'>
    /* <![CDATA[ */
    var pluploadL10n = {"queue_limit_exceeded":"You have attempted to queue too many files.","file_exceeds_size_limit":"%s exceeds the maximum upload size for this site."........

    .

  • Thanks so much for drumming this into me. I’ve finally figured it out. You were correct on both accounts i.e. needed to be inside the loop and also needed to use

    $category->term_id

    So the final code I have is

    <?php
    $cat = get_query_var('cat');
    $args = array(
    	'child_of' => $cat,
    	'orderby' => 'name',
    	'order' => 'ASC'
    	);
    	
    	$categories = get_categories($args);
    	foreach($categories as $category) { 
    	$attachment_id = get_field('cat_half_image', 'category_'. $category->term_id .'');
    	$size = "half-img";
    	echo '<div class="cat-block g_6">';
    	echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . wp_get_attachment_image( $attachment_id, $size );
    	echo '<h2>'. $category->name . '</h2>';
    	echo '</a>';
    	echo '<p>'. $category->description . '</p>';
    	echo '</div>'; }
    ?>
  • Does this look correct? I’m getting good results with it. So, i think i got it.

    $plfsByState = $wpdb->get_results($wpdb->prepare( 
    "
    SELECT pm.meta_value as state, count(*) as postcount 
    FROM $wpdb->posts p
    			join $wpdb->postmeta pm on p.ID = pm.post_id
    where p.post_type = 'plfmaps'
    and p.post_status = 'publish'
    AND meta_key LIKE %s 
    AND meta_value LIKE %s 
    GROUP BY meta_value ASC",
    'locations_%_state',
    '%',
    ""		
    ));

    Thanks for your help. This appears to be showing all of the states, grouped by the state and then showing a count of the total number of results from that state. This is a little tricky with those % signs.

    Question about the query. At the very end, ‘locations_%_state” is the meta_key, and ‘%’ is the meta_value, so then what is the “” at the very end? What does that do?

  • I had similar.

    If you look at the below:

    $rows = $wpdb->get_results($wpdb->prepare( 
    "
    SELECT * 
    FROM wp_postmeta
    WHERE meta_key LIKE %s AND meta_value LIKE %s GROUP BY meta_value ASC",
    'dates_%_available_dates', ''.$year.''.$month.'%', 			
    ""		
    ));	

    You can see that dates_%_available_dates is the repeater field as it went:
    dates_0_available_dates
    dates_1_available_dates
    dates_2_available_dates
    etc

    Using the % allows you to be more dynamic, as you can search on all with the LIKE statement

    This is the link I used:

    It may help you with your code. I most certainly think rewriting your query to something like my example or the one in the link will help though

  • So may be this example may help:

    $posts = get_posts(array(
    	'post_type'		=> 'event',
    	'posts_per_page'	=> -1,
    	'meta_query'		=> array(
    		'relation' => 'OR',
    		array(
    			'key' => 'location',
    			'value' => 'melbourne',
    			'compare' => '='
    		),
    		array(
    			'key' => 'location',
    			'value' => 'sydney',
    			'compare' => '='
    		)
    	)
    ));
     
    if($posts)
    {
    	foreach($posts as $post)
    	{
    		// ...
    	}
    }

    Or maybe something like this (untested):

    $rows = $wpdb->get_results($wpdb->prepare( 
    "
    SELECT * 
    FROM wp_postmeta
    WHERE meta_key LIKE %s GROUP BY meta_key ASC",
    'Locations', 			
    ""		
    ));	

    I’d be inclined to look at some examples. Maybe try running the SQL in PHPMyAdmin and then convert that into the code you need.

  • Hi @panmac

    To query a sub field value, especially if it is an array (checkbox) value, you are going to have to understand completely the SQL going on and be able to debug the code line by line to find any issues. If this is looking to be in the ‘too hard’ barsket, I highly encourage you to hire a freelancer to take a look at this part of your project.

    However, it is possible to write the code you are seeking. Once you have got the LIKE SQL working, you will need to change the line:

    AND meta_value = %s

    to:

    AND meta_value LIKE %s

    and that should allow you to search for values within the array!

    Thanks for being a good sport.

    Thanks
    E

  • Hi @elliot

    sorry for that. so i’m going to explain it to you 🙂

    This is the code i took from your documentation (example 4):

    <?php 
     
    // args
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'event',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'location',
    			'value' => '%Melbourne%',
    			'compare' => 'LIKE'
    		),
    		array(
    			'key' => 'location',
    			'value' => '%Sydney%',
    			'compare' => 'LIKE'
    		)
    	)
    );
     
    // get results
    $the_query = new WP_Query( $args );
     
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endwhile; ?>
    	</ul>
    <?php endif; ?>
     
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

    And the following code is an extract of my entire liveticker code (http://derschlag-handball.de/liveticker). My liveticker works perfect so far, but it takes too much time to fill in all the fields when e.g. somebody makes a goal, etc.

    So what I did was to create another repeater field for each team, where I can write in all players with their numbers. When the match is running I only have to type in the number of the player and in my liveticker the name of the player will be picked from the new created repeater field.

    And there’s my problem. The liveticker repeater field works perfectly (where I always have to type in the players name and number). I also created a second (and third) repeater field for the team-list (number and name of each player). But my code with the modified query of your example (above) does not work:

    <?php
    $number = get_sub_field(‘liveticker_nummer’);
    $name = array(
    ‘numberposts’ => 1,
    ‘post_type’ => ‘liveticker’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘nummer_heim’,
    ‘value’ => $number,
    )
    )
    );
    $name_query = new WP_Query( $name );?>
    
    …
    
    <?php if(get_sub_field(‘ereignis’) == “Tor”) : ?>
    durch <?php if( $name_query->have_posts() ) : while ( $name_query->have_posts() ) : $name_query->the_post(); ?>
    <?php the_field(‘name_heim’);?><?php endwhile; endif; ?> (<?php the_sub_field(‘liveticker_nummer’);?>)</p>
    
    …

    get_sub_field(‘liveticker_nummer’) = get’s the number of the player I tiped in the liveticker repeater field (for each event, like e.g goal)

    ‘nummer_heim’ = the field of my team-list, where the number of the player is listed

    ‘value’ => $number = my goal is that the code uses the number in the liveticker repeater field to query the row of the team-list where also his name is located

    the_field(‘name_heim’) = the name which belongs to $number in the team-list

    I hope you understand what I mean and can help me with that! thanks 🙂

  • seems i have to do this using $wpdb …

    //average for all of post_type by assessor
    
    function q_average_assessor( $fieldname, $posttype, $assessor) {
    
    global $wpdb;
    $repeaterfieldname = 'values_and_outcomes_%_' . $fieldname;
    
     
    	// get all rows from the postmeta table where post_type = $postype AND has a repeater subfield of $fieldname
    	// - http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
            $rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT * 
                FROM wp_postmeta
                INNER JOIN wp_posts ON ( wp_posts.ID = wp_postmeta.post_id ) 
                WHERE wp_posts.post_type=%s
                AND wp_postmeta.meta_key LIKE %s
    
                
                ",
                $posttype,
                $repeaterfieldname
            ));
        
         $coreval = 0;
         $counter = 0;
    	// loop through the results
    	if( $rows )
    	{
    		foreach( $rows as $row )
    		{
    			// for each result, find the 'repeater row number' and use it to load the image sub field!
                preg_match('_([0-9]+)_', $row->meta_key, $matches);
    			$meta_key = 'values_and_outcomes_' . $matches[0] . '_' . $fieldname; // $matches[0] contains the row number!
     
    			//  use get_post_meta 
    			$thisassessor = get_post_meta( $row->post_id, 'assessor', true );
    			if($thisassessor == $assessor) {
    	         $counter++;
    	         $coreval = $coreval + get_post_meta( $row->post_id, $meta_key, true );
                }
    
     
    		}
    	}
    $paverage = $coreval / $counter;
    $average = round($paverage, 1);
    return $average;
    }

    but means a lot of my previous code is wrong. assumption is that for a query on an archive page (ie involving more than one post and repeater fields) get_posts() doesn’t work but $wpdb does?

  • Ok, managed to solve it by swapping the SQl round a little, like so:

    			$rows = $wpdb->get_results($wpdb->prepare( 
    			"
    			SELECT * 
    			FROM wp_postmeta
    			WHERE meta_key LIKE %s AND meta_value LIKE %s ORDER BY meta_value ASC",
    			'dates_%_available_dates', ''.$year.''.$month.'%', 			
    			""		
    			));	
  • Thanks for your input Elliot, it led me on to find my solution.

    I realised I wasn’t using the LIKE operator for my meta_value query and therefore the wildcard values weren’t be recognised. I changed my query to the following and it now returns posts:

    $games_id_array = $wpdb->get_results(
    	$wpdb->prepare( 
    		"
    			SELECT * 
    			FROM wppp_postmeta
    			WHERE meta_key LIKE %s
    			AND meta_value LIKE %s
    		",
    		'contributing_game_creators_%_game_creator_roles',
    		'%'.$search_value.'%'
    	)
    );
  • Ok, this now works as I need it. I wonder if the code can be tidied in anyway but it works. Here’s my code incase it helps anyone else.

    <?php
    #start from current month. Change 6 to however months ahead you want
    for ($x=0; $x<=6; $x++) {
    
    	$date = new DateTime("$x months");
    	$date->modify("-" . ($date->format('j')-1) . " days");
    	#echo $date->format('j, m Y');	
    	$month =  $date->format('m');
    	$year =  $date->format('Y');	
    	#echo 'Month= '.$month .' Year= '.$year.' <br>'; #debug
    
    	$rows = $wpdb->get_results($wpdb->prepare( 
    	"
    	SELECT * 
    	FROM upKeep_postmeta
    	WHERE meta_key LIKE %s
    		AND meta_value LIKE %s
    	",
    	'dates_%_available_dates', // meta_name: $ParentName_$RowNumber_$ChildName
    	#''.$year.''.$month.'%' // meta_value: 20131031 for example
    	''.$year.''.$month.'%' // meta_value: 20131031 for example
    	));
    			
    	// loop through the results
    	if( $rows ) {
    		echo '<h2>'.$date->format('F').' '.$date->format('Y').'</h2>';
    		echo '<ul>';
    		foreach( $rows as $row ) {
    			// for each result, find the 'repeater row number' and use it to load the sub field!
    			preg_match('_([0-9]+)_', $row->meta_key, $matches);
    			$meta_key = 'dates_' . $matches[0] . '_dates'; // $matches[0] contains the row number!
    			?>
    			<li><a href="<?php get_permalink( $row->post_id ); ?>"><?php echo get_the_title( $row->post_id ); ?></a></li>
    			<?php
    		}
    		echo '</ul>';
    	}
    }
    ?>
  • Thanks @elliot for the reply.

    I’ve started to look at the SQL method as I think this may work, however, I’ve come unstuck with the code. I know if I alter the SQL a little I can get it more or less to work for one month.

    So I’ve altered the code to loop for the next 6 months but it only returns 1 result

    Here’s my code:

    for ($x=1; $x<=6; $x++) {
    	#echo "Month is: $x <br>";
    
    	$date = new DateTime("$x months");
    	$date->modify("-" . ($date->format('j')-1) . " days");
    	#echo $date->format('j, m Y');
    	
    	$month =  $date->format('m');
    	$year =  $date->format('Y');
    	
    	echo 'Month= '.$month .' Year= '.$year.' <br>'; #debug
    
    	$rows = $wpdb->get_results($wpdb->prepare( 
    	"
    	SELECT * 
    	FROM upKeep_postmeta
    	WHERE meta_key LIKE %s
    		AND meta_value LIKE %s
    	",
    	'dates_%_available_dates', // meta_name: $ParentName_$RowNumber_$ChildName
    	''.$year.''.$month.'%' // meta_value: 20131031 for example
    	));
    			
    	// loop through the results
    	if( $rows ) {
    		echo '<ul>';
    		foreach( $rows as $row ) {
    			// for each result, find the 'repeater row number' and use it to load the sub field!
    			preg_match('_([0-9]+)_', $row->meta_key, $matches);
    			$meta_key = 'dates_' . $matches[0] . '_dates'; // $matches[0] contains the row number!
    			?>
    			<li><a href="<?php get_permalink( $row->post_id ); ?>"><?php echo get_the_title( $row->post_id ); ?></a></li>
    			<?php
    		}
    		echo '</ul>';
    	}
    
    }

    I think I’m being daft as I believe the above “should” work. Any help is much appreciated!

  • Nope. Still doesn’t work. And yes I have registered the custom image size in the functions file.

    Here is the my full code for the template page. As you can see I am querying two different images “current” and “previous”. I’ve left the query for “previous” as is and it is working (just not with the custom image size. The code you provided is now returning the links with the alt text.

    <div id="scroller">
    
    <?php $args = array(
    	'numberposts' => -1,
    	'post_type' => 'as-investments',
    	'meta_key' => 'investment_status',
    	'meta_value' => 'Current'
    );
    $the_query = new WP_Query( $args ); ?>
    
    <?php if( $the_query->have_posts() ): ?>
    
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    <div class="banner-logo">
      <?php
        $attachment_id = get_field('investment_logo');
        $size = "banner-logo"; // (thumbnail, medium, large, full or custom size)
        $image = wp_get_attachment_image_src( $attachment_id, $size );
        // url = $image[0];
        // width = $image[1];
        // height = $image[2];
      ?>
      <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">
        <img class="investment_logo" alt="Image of <?php the_title(); ?>" src="<?php echo $image[0]; ?>" />
      </a>
    </div>
    
    <?php endwhile; ?>
    
    <?php endif; ?>
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    
    <?php $args = array(
    	'numberposts' => -1,
    	'post_type' => 'as-investments',
    	'meta_key' => 'investment_status',
    	'meta_value' => 'Previous'
    );
    $the_query = new WP_Query( $args ); ?>
    
    <?php if( $the_query->have_posts() ): ?>
    
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    	<div id="banner-logo">
    <a href="<?php the_permalink() ?>" title="<?php printf(__('%s','rys'), get_the_title()) ?>" rel="bookmark" >
    <img class="investment_logo" alt="" src="<?php $image = get_field('investment_logo'); echo($image['sizes']['medium']); ?>" />
    </a>
    	</div>
    
    <?php endwhile; ?>
    
    <?php endif; ?>
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
                                    
    </div>
    

    functions.php file:

    if ( function_exists( 'add_image_size' ) ) add_theme_support( 'post-thumbnails' );
    
    	if ( function_exists( 'add_image_size' ) ) {
    	add_image_size( 'grid-thumb', 79, 79, true );
    	add_image_size( 'profile-thumb', 125, 152, true );
    	add_image_size( 'investment-logo', 151, 98, true );
    	add_image_size( 'banner-logo', 108, 70, true );
    }
    

    See attached screenshot for custom fields.

    Just to note: I am calling this image in two different locations on the site. One is where the image size needs to be 151×98 (investment logo) and then again for 108×70 (banner logo). I’m not sure if it matters but in the custom fields I have the “Investment Logo” selected.

Viewing 25 results - 251 through 275 (of 281 total)