WordPress.org

Ready to get started?Download WordPress

Forums

gengo get-a-post function (2 posts)

  1. grosbouff
    Member
    Posted 6 years ago #

    I wrote a function in gengo_extra_functions.php, to be able to catch a translated post from an ID; sort of get-a-post plugin.
    If the function can't find a translation, it shows the default post.

    // Sponsored by Gordie Lachance
    // Outputs the translation of the post ID.
    function gengoGetAPost($monpost) {
    	global $post, $gengo, $wpdb;
    	$now = current_time('mysql');
    	$where_posts = "WHERE p.post_date <= '$now' AND p.post_status = 'publish'";
    	$language_ids = implode(',', $gengo->language_preference_id);
    
    	$post = $wpdb->get_row("SELECT p.* FROM $wpdb->posts  AS p INNER JOIN $gengo->post2lang_table AS p2l ON p.ID = p2l.post_id INNER JOIN $gengo->post2lang_table AS p3l ON p3l.post_id='".$monpost."' ".$where_posts." AND p2l.translation_group=p3l.translation_group AND p2l.language_id IN ($language_ids) LIMIT 1");
    
    	if (!$post) { // if there is no translation, show the original post
    
    		$where_posts .= " AND p.id='".$monpost."'";
    
    		$post = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p ".$where_posts." LIMIT 1");
    
    	}
    	get_post_custom($post->ID);
    	setup_postdata($post);
    }
  2. grosbouff
    Member
    Posted 6 years ago #

    there is errors in the previous script.
    I have a new version. The problem is that you have to give the function the post id value of phpmyadmin.
    Don't know if it's normal, but in my wordpress, posts_ids in the admin pannels and posts_ids in sql tables are not the same.

    ***IN GENGO_EXTRA_FUNCTIONS.PHP***

    // Sponsored by Gordie Lachance
    // Outputs the translation of the post ID.
    function gengo_folio_newID($monid) {
    	global $post, $gengo, $wpdb;
    	$now = current_time('mysql');
    	$where_posts = "WHERE p.post_date <= '$now' AND p.post_status = 'publish'";
    	$language_ids = implode(',', $gengo->language_preference_id);
    
    	$query="SELECT p.id FROM $wpdb->posts  AS p INNER JOIN $gengo->post2lang_table AS p2l ON p.ID = p2l.post_id INNER JOIN $gengo->post2lang_table AS p3l ON p3l.post_id='".$monid."' ".$where_posts." AND p2l.translation_group=p3l.translation_group AND p2l.translation_group!=0 AND p2l.language_id IN ($language_ids) LIMIT 1";
    
    	$newID = $wpdb->get_var($query);
    
    	if (!$newID) {
    		$newID = $monid;
    	}
    
    	return $newID;
    
    }

    ***IN TEMPLATE :

    <?php
    get_a_post(gengo_folio_newID(145));
    ?>
    	<div class="post-intro" id="intro">
    		<h3><?php the_title();?></h3>
    		<?php the_content(); ?>
    		<p class="postmetadata"><?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    	</div>

Topic Closed

This topic has been closed to new replies.

About this Topic