Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

RESOLVED by me :-) adjacent post with same meta-key and same meta_value WordPress

  • REFUND REQUESTED

i need a function to display the next and previous post (title, permalink, date, content) in single.php

next and previous post must have the same meta_key and the same meta_value of the current post

the function could be something like that


my_adjacent_post($order=ASC, $meta_key, $meta_value)





--------------------------------------------------------------
<strong>EDIT</strong>


i have found the solution :-)

I wrote this function :
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// fonction pour afficher la navigation vers l'article suivant ou précedent en fonction de sa meta_value et sa meta_key
// sebastien | french wordpressdesigner | [email protected]
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function my_adjacent_post($meta_key, $meta_value , $nextprev = 'next' ){

global $post;
$date = $post->post_date;

global $wpdb;

if($nextprev == 'next' ) {
$sign = '>';
$order= 'ASC';
}
elseif($nextprev == 'prev' ) {
$sign = '<';
$order= 'DESC';
}

$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '".$meta_key."'
AND $wpdb->postmeta.meta_value = '".$meta_value."'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_date ".$sign." '".$date."'
ORDER BY $wpdb->posts.post_date ".$order."
LIMIT 1";



$postData = $wpdb->get_results($querystr, OBJECT);
//print_r($postData);

if(empty($postData)) {
echo "this array is empty, i could use a default content.";
} else {
foreach($postData as $article) {
$list = "this is the ".$nextprev." post";
$list .= "<BR>ID : ";
$list .= $article->ID;
$list .= "<BR>url : ";
$list .= get_permalink($article->ID);
$list .= "<BR>title : ";
$list .= $article->post_title;
$list .= "<BR>date : ";
$list .= $article->post_date;
echo $list;
}
}
}


and the function to use :

$meta_key = 'brand';
$meta_value = 'SONY';
echo "prev : ";
my_adjacent_post($meta_key,$meta_value,'prev');
echo "next : ";
my_adjacent_post($meta_key,$meta_value,'next');

Answers (2)

2012-08-24

Abdelhadi Touil answers:

Hi.
Wordpress has a built in function that displays next and previous posts:
[[LINK href="http://codex.wordpress.org/Next_and_Previous_Links"]]http://codex.wordpress.org/Next_and_Previous_Links[[/LINK]]
what do you want exactly?


Sébastien | French WordpressDesigner comments:

the simply way : read my question


Abdelhadi Touil comments:

Do you think I didn't read your question? Actully I'v read it but I'd like to have more explination. Hope others help you. Good luck.

2012-08-24

Arnav Joy answers:

tyrtry this

functions.php

<?php

function my_adjacent_post($postID , $meta_key, $meta_value , $nextprev ){
global $wpdb;
if($nextprev == 'next' )
$sign = '>';
else
$sign = '<';

$querystr = "SELECT wposts.ID,wposts.post_title FROM $wpdb->posts wposts LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id WHERE wposts.ID ".$sign." ".$postID." AND wpostmeta.meta_key = '".$meta_key."'
AND wpostmeta.meta_value = '".$meta_value."' AND wposts.post_status = 'publish' ";

$postData = $wpdb->get_results($querystr, OBJECT);
if(!empty($postData)){
foreach($postData as $pData)
$postArr[] = get_permalink($pData->ID);
$postArr[] = $pData->post_title;
return '<a href="'.$postArr[0].'">'.$postArr[1].'</a>';
}

}
?>


then call it as


<?php
$meta_key = 'mail_order';
$meta_value = 'test';
my_adjacent_post(get_the_ID(),$meta_key,$meta_value,'next');
my_adjacent_post(get_the_ID(),$meta_key,$meta_value,'prev');
?>


Sébastien | French WordpressDesigner comments:

i have test with my meta value and my meta key, but the array $postData return an empty array.



Arnav Joy comments:

change this line

return '<a href="'.$postArr[0].'">'.$postArr[1].'</a>';


to

echo '<a href="'.$postArr[0].'">'.$postArr[1].'</a>';


Sébastien | French WordpressDesigner comments:

that is not the problem

$postData is empty

print_r($postData) return an empty array


Arnav Joy comments:

Have you checked that the other post hasve same meta key and and same meta value as the current post


i have three post with the ID as follows

1706
1716 ----> current visiting post
1721


so all the three posts have same meta key and same meta values

meta_key = 'mailer_order'
meta_value = 'test'

and when i visited post 1716 then it gives me

prev post=> 1706
next post => 1721

please check and let me know



Sébastien | French WordpressDesigner comments:

i have 4 posts with the meta_key "brand" and the meta_value "DIESEL"

in my single.php i write :
$meta_key = 'marque';
$meta_value = 'DIESEL';
my_adjacent_post(get_the_ID(),$meta_key,$meta_value,'next');
my_adjacent_post(get_the_ID(),$meta_key,$meta_value,'prev');


but nothing is displayed.

I add in your function :
if(empty($postData)) {echo "this array is empty";}

and the output on my site is :
this array is emptythis array is empty


Arnav Joy comments:

you posts status are 'published' ?



Sébastien | French WordpressDesigner comments:

and i use wp 3.4.1



Arnav Joy comments:

same.


Sébastien | French WordpressDesigner comments:

ok, i have find my mistake
the value must be an ID and not the name of the brand, sorry for this confusion.

So, your function is ok.
But there is a problem. If the url is ok, the label of the link is wrong.
and if the label is ok, the url is wrong


Arnav Joy comments:

change this

$postArr[] = get_permalink($pData->ID);

$postArr[] = $pData->post_title;

to


$postArr[] = get_permalink($pData->ID);

$postArr[] = get_the_title($pData->ID); // this lijne is modified


Sébastien | French WordpressDesigner comments:

don't work
one time the output is the title, one time the output in an url...


Sébastien | French WordpressDesigner comments:

probable you must change the order (asc/desc) if it's next OR prev, no ??
and add a LIMIT = 1 to have a light request, no ?


Sébastien | French WordpressDesigner comments:

i feel that posts are sorted by ID in you request... no ?
Posts must be sorted by date of course.


Sébastien | French WordpressDesigner comments:

in fact i need just a request to display the next post (date) which have a specific meta_value and a specific meta_key
and the previous post (date) which have a specific meta_value and a specific meta_key :-)


Arnav Joy comments:

here is the updated function , please check it

<?php

function my_adjacent_post($postID , $meta_key, $meta_value , $nextprev ){

global $wpdb;

if($nextprev == 'next' )

$sign = '>';

else

$sign = '<';



$querystr = "SELECT wposts.ID,wposts.post_title FROM $wpdb->posts wposts LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id WHERE wposts.ID ".$sign." ".$postID." AND wpostmeta.meta_key = '".$meta_key."'

AND wpostmeta.meta_value = '".$meta_value."' AND wposts.post_status = 'publish' ";



$postData = $wpdb->get_results($querystr, OBJECT);

if(!empty($postData)){

$i = 0;
foreach($postData as $pData) {

$postArr[$i++] = get_permalink($pData->ID);

$postArr[$i] = $pData->post_title;

echo '<a href="'.$postArr[0].'">'.$postArr[1].'</a>';

return;

}

}



}

?>


Sébastien | French WordpressDesigner comments:

thank you very much but your function sorts the posts by id not by date.

I have edited my question because i have found the solution. You can read my function in my edit.
Thanx