0

I have a post type Issue, that has meta values in form of associative arrays (that are saved as serialized strings to database). They look somewhat similar to this:

    [article_reviews] => Array (
    [0] => Array (
        [title] => Article 1
        [url] => http://itvar.dev/2015/11/fenicka-kolonizace-s-moreplavci-v-rudem/
        [authors] => Author 1, Author 2
        [rubriky] => Category 1
    ),
    [1] => Array (
        [title] => Article 2
        [url] => http://itvar.dev/2015/11/milos-vorac-nebrat-ceske-slovo-nadarmo/
        [authors] => Author 3
        [rubriky] => Category 2
    )
),

[article_essays] => Array (
    [0] => Array (
        [title] => Article A
        [url] => http://itvar.dev/2015/11/example-example
        [authors] => Author 1, Author B
        [rubriky] => Category A
    ),
    [1] => Array (
        [title] => Article B
        [url] => http://itvar.dev/2015/11/example-example
        [authors] => Author C
        [rubriky] => Category B
    )
)

The problem is I need to search post type Issue by this data. That means I need to build a wp_query with tax_query, that can search in associative arrays. Somewhat similar to this:

$search_query = 'Author 1';
$args = array(
    'post_type' => 'issue',
    'meta_query' => array(
        array(
            'key'       => 'article_reviews',
            'value'     => $search_query,
            'relation'  => 'LIKE'
        ),
        array(
            'key'       => 'article_essays',
            'value'     => $search_query,
            'relation'  => 'LIKE'
        )
    )
);

// should find this post, because $article_reviews[0], $article_essays[0]

Background

This is already answered - for example here - as impossible. But as I don't see any way around the problem I'm trying to solve, I would like to ask anyway. And I hope, that this question provide some value for anyone else with similiar situation as I have.

I am building a website for a magazine about literature. 25% of the content from print issue gets to be online, 75% stays in print only. However each Issue (custom post type issue) comes with a list of all 100% articles (meta fields article_reviews, articles_essays, etc.; 25% clickable, 75% not) that needs to be searchable, so the web visitor will find the specific issue containing Author 1.

On back-end, I currently use metabox with repeatable fields (noone can really now, if there will be 2 or 20 reviews in current issue), that save data as associative arrays.

I am very much opened to alternative solutions, but:

  • "just splitting the array" as linked question suggests is not really an option for me, as data for Article 1 etc. need to be "glued" together somehow
  • I would prefer to preserve the arrays, so I would not have to make brutal changes to my back-end. If there really are no ways around that, than ok.

Thank you!

  • You can't search serialized strings in MySQL in any efficient reliable way. What you've done, if it was you who created this problem, is simply terrible database design. Rebuild it. – s_ha_dum Nov 20 '15 at 5:17
  • You could flatten the arrays and store the "glue" separately, basically another field that ties all of the related IDs together. Maybe a better approach would be to make reviews and essays be additional post types, then their attributes can be individual meta values. – Milo Nov 20 '15 at 5:20
  • @s_ha_dum As I've said, I am open to rebuilding the logic of storing the data, if it meets stated criteria. Do you have any suggestions? – Petr Cibulka Nov 20 '15 at 5:44
  • @Milo Most of the Articles in the list do not point to specific posts, as the articles are not online. The data are merely a list of articles (with authors, titles, etc.), some of them have links to website. Sorry if it's not clear from the question. About the separate glue - can you provide me some simple example? – Petr Cibulka Nov 20 '15 at 5:46
  • 1
    A post type doesn't have to be thought of as a post, it's just a way to store an object with meta data. An example of this is the WordPress nav menu system- menu items are posts of the nav_menu post type, and menu item attributes are post meta data. You can use the post parent field to connect those objects to issues. The glue idea is to save all of the titles, urls, etc., as individual meta items, then create an additional field for each which is an array of IDs that stores the relationship between specific fields- which title goes with which url, etc.. I don't like that idea as much. – Milo Nov 20 '15 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.