I’ve recently moved my fishkeeping “species profiles” over from a custom DB to a customised WordPress custom post type.
I did this by creating a PHP file which accessed the former database, extracted the appropriate information and inserted it into the WP database.
This has worked for all of the profiles and all attached media, but some of my references
(key of the meta field) don’t display in back nor front-end, despite being in the database.
Here’s one of the references that has been pulled over which IS displaying:
Array
(
[0] => Array
(
[ref_doc_title] => The fighting fishes (Teleostei: Osphronemidae: Genus Betta) of Singapore, Malaysia and Brunei.
[ref_pub_year] =>
[ref_page_nums] => Raffles Bull. Zool. Supplement (13):43-99. 2005
[ref_publishers] =>
[ref_authors] => Tan, H.H. and P.K.L. Ng.
)
[1] => Array
(
[ref_doc_title] => The fishes of Danau Sentarum National Park and the Kapuas Lakes area, Kalimantan Barat, Indonesia.
[ref_pub_year] =>
[ref_page_nums] => Raffles Bull. Zool. Supplement (13):139-173. 2005
[ref_publishers] =>
[ref_authors] => Kottelat, M. and E. Widjanarti.
)
[2] => Array
(
[ref_doc_title] =>
[ref_pub_year] =>
[ref_page_nums] => www.fishbase.org
[ref_publishers] =>
[ref_authors] =>
)
[3] => Array
(
[ref_doc_title] =>
[ref_pub_year] =>
[ref_page_nums] => www.ibcbettas.org
[ref_publishers] =>
[ref_authors] =>
)
[4] => Array
(
[ref_doc_title] =>
[ref_pub_year] =>
[ref_page_nums] => www.petfrd.com
[ref_publishers] =>
[ref_authors] =>
)
)
And it looks like this in the database:
a:5:{i:0;a:5:{s:13:"ref_doc_title";s:94:"The fighting fishes (Teleostei: Osphronemidae: Genus Betta) of Singapore, Malaysia and Brunei.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:47:"Raffles Bull. Zool. Supplement (13):43-99. 2005";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:24:"Tan, H.H. and P.K.L. Ng.";}i:1;a:5:{s:13:"ref_doc_title";s:98:"The fishes of Danau Sentarum National Park and the Kapuas Lakes area, Kalimantan Barat, Indonesia.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:49:"Raffles Bull. Zool. Supplement (13):139-173. 2005";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:31:"Kottelat, M. and E. Widjanarti.";}i:2;a:5:{s:13:"ref_doc_title";s:0:"";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:16:"www.fishbase.org";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:0:"";}i:3;a:5:{s:13:"ref_doc_title";s:0:"";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:17:"www.ibcbettas.org";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:0:"";}i:4;a:5:{s:13:"ref_doc_title";s:0:"";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:14:"www.petfrd.com";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:0:"";}}
Then I have one which isn’t working:
Array
(
[0] => Array
(
[ref_doc_title] => Revision of the family Badidae (Teleostei: Perciformes), with description of a new genus and ten new species.
[ref_pub_year] =>
[ref_page_nums] => Ichthyol. Explor. Freshwat. 13(4): 295-372.
[ref_publishers] =>
[ref_authors] => Kullander, S.O. and R. Britz. 2002
)
[1] => Array
(
[ref_doc_title] => Evolutionary and biogeographic patterns of the Badidae (Teleostei: Perciformes) inferred from mitochondrial and nuclear DNA sequence data.
[ref_pub_year] =>
[ref_page_nums] => Mol. Phylogen. and Evol. 32(3): 1010-1022.
[ref_publishers] =>
[ref_authors] => Rüber, L., R. Britz, S. O. Kullander and R. Zardoya. 2004
)
)
And this is how it looks in the database:
a:2:{i:0;a:5:{s:13:"ref_doc_title";s:110:"Revision of the family Badidae (Teleostei: Perciformes), with description of a new genus and ten new species. ";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:44:"Ichthyol. Explor. Freshwat. 13(4): 295-372. ";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:34:"Kullander, S.O. and R. Britz. 2002";}i:1;a:5:{s:13:"ref_doc_title";s:138:"Evolutionary and biogeographic patterns of the Badidae (Teleostei: Perciformes) inferred from mitochondrial and nuclear DNA sequence data.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:42:"Mol. Phylogen. and Evol. 32(3): 1010-1022.";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:57:"Rüber, L., R. Britz, S. O. Kullander and R. Zardoya. 2004";}}
The code that I’m using in the back-end is as follows:
function meta_references() {
global $post;
if ($post) {
$references = get_post_meta( $post->ID , 'references', TRUE );
if (get_post_meta( $post->ID, 'ref_type_desc', TRUE) != "") {
$ref_type_desc = get_post_meta( $post->ID, 'ref_type_desc', TRUE);
} else {
$ref_type_desc = -1;
}
}
?>
<div class="meta_control normal">
<div id="references_wrapper">
<p>Click <a href="javascript:void(0);" id="add_reference">here</a> to add a new reference.</p>
<?php
if (!empty($references)) {
$count = count($references);
for ($i = 1; $i <= $count; $i++) {
if ($ref_type_desc != -1 && $i == $ref_type_desc) {
$this->print_reference($i, $references[$i-1], TRUE);
} else {
$this->print_reference($i, $references[$i-1]);
}
}
} else {
$this->print_reference(1);
}
?>
<p>Click <a href="javascript:void(0);" id="uncheck_ref_radio">here</a> to unset the Type Description buttons.</p>
</div>
</div>
<?php
}
function print_reference($i, $array = NULL, $type_desc = NULL) {
// any information below must be changed in references.js
?>
<div class="reference">
<h3>Reference <?php echo $i; ?></h3>
<div class="line double">
<label>Author(s)</label> <input type="text" name="ref_authors[]" value="<?php if (isset($array["ref_authors"])) { echo $array["ref_authors"]; } ?>" />
<label>Publisher</label> <input type="text" name="ref_publishers[]" value="<?php if (isset($array["ref_publishers"])) { echo $array["ref_publishers"]; } ?>" />
</div>
<div class="line double">
<label>Publication Year</label> <input type="text" name="ref_pub_year[]" value="<?php if (isset($array["ref_pub_year"])) { echo $array["ref_pub_year"]; } ?>" />
<label>Page Number(s)</label> <input type="text" name="ref_page_nums[]" value="<?php if (isset($array["ref_page_nums"])) { echo $array["ref_page_nums"]; } ?>" />
</div>
<div class="line"><label>Document Title</label> <input type="text" name="ref_doc_title[]" value="<?php if (isset($array["ref_doc_title"])) { echo $array["ref_doc_title"]; } ?>" /></div>
<div class="line"><label style="width:150px;">Type Description?</label> <input type="radio" class="ref_type_desc" name="ref_type_desc" style="padding-top:5px;margin-top:5px;" value="<?php echo $i; ?>"<?php if ($type_desc == TRUE) { echo 'checked = "checked"'; } ?> /></div>
<div style="clear:both;line-height:0;margin:0;padding:0;"> </div>
<p>Click <a href="javascript:void(0);" id="<?php echo $i; ?>_del_reference">here</a> to remove Reference #<?php echo $i; ?>.</p>
</div>
<?php
}
And the code that I’m using on the front-end is:
if (get_post_meta($post->ID, 'references', TRUE) != "") {
$references = get_post_meta($post->ID, 'references', TRUE);
if (strlen($references[0]["ref_doc_title"]) > 0) {
echo '<h2 id="references" class="profile_literaturecited">References</h2>' . "n";
echo '<ol>';
if (get_post_meta($post->ID, 'ref_type_desc', TRUE) != "") {
$type_desc = get_post_meta($post->ID, 'ref_type_desc', TRUE);
$count = count($references);
for ($i = 1; $i <= $count; $i++) {
if ($i == $type_desc) {
$n = $i - 1;
$r = $references[$n];
echo '<li class="type_desc">';
echo '<strong>' . $r["ref_authors"] . ', ' . $r["ref_pub_year"] . '</strong>';
echo ' - ' . $r["ref_publishers"] . ': ' . $r["ref_page_nums"];
echo '<br />' . $r["ref_doc_title"];
echo '</li>';
unset($references[$n]);
}
}
}
// user defined sort comparison. use lowercase to ensure sort is accurate
function cmp($a, $b)
{
$al = strtolower($a["ref_authors"]);
$bl = strtolower($b["ref_authors"]);
if ($al == $bl) {
return 0;
}
return ($al > $bl) ? +1 : -1;
}
usort($references, "cmp");
foreach ($references as $r) {
echo '<li>';
echo '<strong>' . $r["ref_authors"] . ', ' . $r["ref_pub_year"] . '</strong>';
echo ' - ' . $r["ref_publishers"] . ': ' . $r["ref_page_nums"];
echo '<br />' . $r["ref_doc_title"];
echo '</li>';
}
echo '</ol>';
}
}
Nothing at all displays in the back-end or front-end for any of these “non-working” references, yet the entries are in the database with the correct post ID and meta_key.
So I’m wondering, is this serialized array (the non-working one) somehow malformed for WordPress usage? I don’t think it’s malformed by PHP standards as I can print_r
it perfectly using the unserialize
command.
Any help would be much appreciated, I think I’m going crazy!
EDIT for Chip
Here is the (abridged) output of your code for one of the species profiles which DOESN’T display the references. I’ve removed the lengthy snippets of other meta fields, but left two other meta printouts in to show how they return.
array(19) {
["genus"]=>
array(1) {
[0]=>
string(20) "Yasuhikotakia"
}
["species"]=>
array(1) {
[0]=>
string(16) "splendida"
}
….
["references"]=>
array(1) {
[0]=>
string(4117) "a:12:{i:0;a:5:{s:13:"ref_doc_title";s:74:"Genetic variation of five species of Yasuhikotakia in Thailand using AFLP.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:48:"Journal of Agricultural Technology 6(4): 761-766";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:60:"Arthainsee, A., T. Leamkom, A. Jutagate and S. Chundum. 2010";}i:1;a:5:{s:13:"ref_doc_title";s:48:"The Botia modesta complex (Cobitidae, Botiinae).";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:22:"Ichthyofile No. 1: 1-8";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:14:"Grant, S. 2001";}i:2;a:5:{s:13:"ref_doc_title";s:15:"Fishes of Laos.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:52:"WHT Publications Ltd., Colombo 5, Sri Lanka. 198 p. ";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:17:"Kottelat, M. 2001";}i:3;a:5:{s:13:"ref_doc_title";s:168:"Botia kubotai, a new species of loach (Teleostei: Cobitidae) from the ataran River basin (Myanmar), with comments on botiinae nomenclature and diagnosis of a new genus.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:17:"Zootaxa 401: 1-18";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:17:"Kottelat, M. 2004";}i:4;a:5:{s:13:"ref_doc_title";s:96:"Sixty million years of evolution. Part one: family Botiidae (Pisces: Ostariophysi: Cobitoidea). ";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:81:"Travaux du Museum d'Histoire Naturelle "Grigore Antipa" 44: 309-333";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:19:"Nalbant, T. T. 2002";}i:5;a:5:{s:13:"ref_doc_title";s:128:"Hymenphysa, Hymenophysa, Syncrossus, Chromobotia and other problems in the systematics of Botiidae. A reply to Maurice Kottelat.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:82:"Travaux du Museum d'Histoire Naturelle "Grigore Antipa" 47: 269-277.";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:19:"Nalbant, T. T. 2004";}i:6;a:5:{s:13:"ref_doc_title";s:90:"Botia splendida, a new species of loach (Pisces: Cobitidae) from the Mekong Basin in Laos.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:46:"The Raffles Bulletin of Zoology 43(2): 463-467";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:19:"Roberts, T. R. 1995";}i:7;a:5:{s:13:"ref_doc_title";s:147:"Molecular phylogeny of the Southeast Asian freshwater fish family Botiidae (Teleostei: Cobitoidea) and the origin of polyploidy in their evolution.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:49:"Molecular Phylogenetics and Evolution 39: 529-541";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:53:"Šlechtová, V., J. Bohlen, J. Freyhof and P. Ráb. 2006";}i:8;a:5:{s:13:"ref_doc_title";s:188:"Families of Cobitoidea (Teleostei; Cypriniformes) as revealed from nuclear genetic data and the position of the mysterious genera Barbucca, Psilorhynchus, Serpenticobitis and Vaillantella.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:54:"Molecular Phylogenetics and Evolution 44(3): 1358-1365";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:44:"Šlechtová, V., J. Bohlen and H. H. Tan. 2007";}i:9;a:5:{s:13:"ref_doc_title";s:72:"Two new species of the cobitid genus [ì]Botia from the Lao Mekong Basin.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:46:"Japanese Journal of Ichthyology 42(2): 147-155";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:25:"Taki, Y. and A. Doi. 1995";}i:10;a:5:{s:13:"ref_doc_title";s:126:"Phylogeny of the East Asian botiine loaches (Cypriniformes, Botiidae) inferred from mitochondrial cytochrome b gene sequences.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:29:"Hydrobiologia 544(1): 249-258";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:44:"Tang, Q., B. Xiong, X. Yang and H. Liu. 2005";}i:11;a:5:{s:13:"ref_doc_title";s:177:"Comparison of evolutionary rates in the mitochondrial DNA cytochrome b gene and control region and their implications for phylogeny of the Cobitoidea (Teleostei: Cypriniformes).";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:52:"Molecular Phylogenetics and Evolution 39(2): 347-357";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:46:"Tang, Q., H. Liu, R. Mayden and B. Xiong. 2006";}}"
}
["_thumbnail_id"]=>
array(1) {
[0]=>
string(5) "25351"
}
["attached_media"]=>
array(1) {
[0]=>
string(156) "a:12:{i:0;i:25351;i:1;i:25352;i:2;i:25353;i:3;i:25354;i:4;i:25355;i:5;i:25356;i:6;i:25357;i:7;i:25358;i:8;i:25359;i:9;i:253510;i:10;i:253511;i:11;i:253512;}"
}
On one of the profiles that works, it displays in a similar manner:
["references"]=>
array(1) {
[0]=>
string(1145) "a:5:{i:0;a:5:{s:13:"ref_doc_title";s:94:"The fighting fishes (Teleostei: Osphronemidae: Genus Betta) of Singapore, Malaysia and Brunei.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:47:"Raffles Bull. Zool. Supplement (13):43-99. 2005";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:24:"Tan, H.H. and P.K.L. Ng.";}i:1;a:5:{s:13:"ref_doc_title";s:98:"The fishes of Danau Sentarum National Park and the Kapuas Lakes area, Kalimantan Barat, Indonesia.";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:49:"Raffles Bull. Zool. Supplement (13):139-173. 2005";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:31:"Kottelat, M. and E. Widjanarti.";}i:2;a:5:{s:13:"ref_doc_title";s:0:"";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:16:"www.fishbase.org";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:0:"";}i:3;a:5:{s:13:"ref_doc_title";s:0:"";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:17:"www.ibcbettas.org";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:0:"";}i:4;a:5:{s:13:"ref_doc_title";s:0:"";s:12:"ref_pub_year";s:0:"";s:13:"ref_page_nums";s:14:"www.petfrd.com";s:14:"ref_publishers";s:0:"";s:11:"ref_authors";s:0:"";}}"
}
** FINAL EDIT **
@goldenapples was correct with his answer.
I’ve now opened a new question on Stackoverflow (as it isn’t WordPress specific).
https://stackoverflow.com/questions/9088910/migrating-data-from-latin1-charset-to-utf-8
If any of you can help it would be much appreciated!
See my comment on your question above. I just realized that if you’re ending up with those special characters in the database, most likely you’re not sanitizing the data before saving it into the database. Try running
htmlentities()
over all the values that you’re saving into that serialized array, and see if you end up with a readable result.Have you read the Codex entry for
get_post_meta()
?It notes that meta values that are serialized arrays are a bit of a special case, primarily impacting the
$single
parameter in theget_post_meta($post_id, $key, $single)
call:You do appear to be handling this properly, though.
To see if WordPress can read the array properly, try this:
I suspect that this will return your array; in which case, you simply need to step into it:
But if it doesn’t return your array, we’ll need to figure out where else the problem might be.
Edit
That means that
get_post_meta()
itself isn’t returning anything for thereferences
meta field for the post in question. Let’s find out what WordPress does find as custom meta data for this post:Does
references
show up in this var_dump? If so, what does it look like?Edit 2
Based on your var_dump results:
We can tell that
references
is getting stored as a serial array string. That may or may not be as intended; do you get the same thing from the posts with workingreferences
meta data?It looks like you’re going to need to do one of two things:
I don’t know if the latter will actually work, since
get_post_meta()
didn’t even return anything forreferences
.Ultimately, I think the answer is that your import script imported some data serialized, and some data unserialized. The solution will probably be to re-import your data, so that all data are imported consistently.