0

I'm looking to query my posts via a custom field that I've created on my page. But the custom field is an array of values. So the custom field 'country' could look like this

country => usa,africa,united kingdom,poland

What's confusing me is how to query posts with a custom field which is an array seperated with ','?

I've tried quite a few solutions on here with no success.

Any help would be greatly appreciated!

2 Answers 2

0

Did you write "usa,africa,united kingdom,poland" as the value for one key?

That would be a string and not an array.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, but wouldn't it need to be separated out if I was writing a custom query for tags?
Is their a reason you don't want to enable tags for that post type? stackoverflow.com/questions/10245071/…
Posts have tags but what I'm wanting to do is have a custom field attached to a page which has a string of tags in it separated by a comma. Then query for posts on that page which have those specific page tags.
I got ya. Well you could try explode() on that field (which I would advise against) which would break the string into an array. You would use the comma as the delimiter. The problem would be that your database query would retrieve every post and then explode() each time that field is encountered. Have you thought about custom taxanomies for that post? It's very similar to tags. The benefit would be that the functionality is built into wordpress. justintadlock.com/archives/2009/05/06/…
Are these better than tags at all? I currently have tags in each post. Is there any better way of doing it rather than having a custom field on the page? Is there a way I can do it via templates etc?
|
0

https://codex.wordpress.org/Taxonomies

function people_init() {
    // create a new taxonomy
    register_taxonomy(
        'countries',
        'post',
        array(
            'label' => __( 'Countries' ),
            'rewrite' => array( 'slug' => 'countries' ),
            'capabilities' => array(
                'assign_terms' => 'edit_guides',
                'edit_terms' => 'publish_guides'
            )
        )
    );
}
add_action( 'init', 'countries_init' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.