• Resolved nesnejairam

    (@nesnejairam)


    I have created a site, where the editor has a posibility to add related posts to a post.

    Now, when a page is shown, all related posts are presented below the content. A related post is presented by a title, a thumbnail and some text. The text comes from a custom field (_custom_introduce_text) that I pick up using this:
    $manchet = $wpdb->get_results("SELECT meta_value FROM wp_FreshFarm_postmeta WHERE post_id=".$key->ID." and meta_key='_custom_introduce_text'", 'OBJECT');

    I will start by saying that this actually worked before upgrading to 3.5.1, so I’m at a loss why it’s not working anymore.

    If I “hard code” the query to e.g.
    $manchet = $wpdb->get_results("SELECT meta_value FROM wp_FreshFarm_postmeta WHERE post_id=476 and meta_key='_custom_introduce_text'", 'OBJECT');
    then a result is returned.

    If I write $key to the screen this is not empty.

    But whenever $key is part of the query, nothing is returned from the database.

    This is the “full” code that I’m processing:

    <?php
    $related_posts = MRP_get_related_posts( $post->ID );
        if( $related_posts ) {?><h2>Relaterede artikler</h2>
    				<ul class="opsamlingPost">
    <?php foreach( $related_posts as $key=>$value ) { ?>
    						<li>
    							<a href="<?php echo get_permalink($key); ?>">
    								<?php echo get_the_post_thumbnail($key, array(220,220)); ?>
    								<h2><?php echo get_the_title($key); ?></h2>
    		<?php
    		$manchet = $wpdb->get_results("SELECT meta_value FROM wp_FreshFarm_postmeta WHERE post_id=".$key->ID." and meta_key='_custom_introduce_text'", 'OBJECT'); 
    
    		if( $manchet )
    			foreach( $manchet as $my_manchet) { ?>
            <p><?php echo $my_manchet->meta_value; ?></p>
            <?php } ?>
    							</a>
    							</li>
    <?php }
    ?>
    				</ul>

    Any help is much appreciated 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    It may have been your host changing PHP versions rather than a WP upgrade causing this issue. Others have had version issues when assigning array element values to structures like $key=>$value. Depending on the contents of $related_posts, this construct is not required. Try using $key alone.

    Then either all following uses of $key will need to change to $key->ID or the use in the query should become just $key, depending on the content of $related_posts.

    Thread Starter nesnejairam

    (@nesnejairam)

    Perfect – that solved my problem. I had not thought of that, because nothing was wrong with the other queries, e.g.
    “SELECT meta_value FROM wp_FreshFarm_postmeta WHERE post_id=”.$pageChild->ID.” and meta_key=’_custom_introduce_text'”

    Thank you so much for your help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problems with database query after upgrading’ is closed to new replies.