0

Firstly, what I am trying to do is display posts from Category x on posts from Category y where text from a custom field on post from Cat y matches text from one of 2 custom fields on cat x.

I am using Advanced Custom Fields and have a custom text field on each post from cat x and y to store a list of attributes. e.g. red,car,sports,alloys.

I then want to check to see if they match up between posts and pull in the post where they do.

The trouble I am having is getting the custom field value (text field) and then passing it into the array) The text field contains comma seperated values e.g. "car", "red" if I print the field it shows this. I have tried converting the string to an array using explode, but still it is returning nothig. If I manually add "car", "red" to the array, it works.

I am currently using WP_Query, my latest query is:

 <?php $list = get_field( "main_attributes" ); $arr = array($list); $array = explode(',', $list); $args = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'style_atrributes', 'value' => $array, 'compare' => 'IN' ), array( 'key' => 'car_atrributes', 'value' => '$array', 'compare' => 'IN' ), ), ); $custom_query = new WP_Query($args); while($custom_query->have_posts()) : $custom_query->the_post(); ?> <div <?php post_class(); ?> id="post-<?php the_ID(); ?>"> <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <?php the_content(); ?> </div> <?php endwhile; ?> <?php wp_reset_postdata(); // reset the query ?> 
14
  • hmmm... you are using text as part of an array? Where are you storing the "red" and "car" data and how are you retrieving it. You haven't posted enough code.
    – s_ha_dum
    CommentedMay 15, 2015 at 14:06
  • I am storing "red" and "car' etc as text in a custom field on each post page. This is the full block of code I have place in the single.php see above.
    – pdg87
    CommentedMay 16, 2015 at 11:56
  • The fact that it is returning all the pages makes me think it is working, but the query is wrong. Could it be returning false positives? I want it to check the fill word before comma, e.g. red, green, but would it return both because they both include letter 'e' Sorry my php is weak!
    – pdg87
    CommentedMay 16, 2015 at 12:50
  • If I change compare to 'compare' => 'LIKE' and type the array as 'value' => array("red", "car") it works. But if I change the array to the field its doesnt. Even thought if I print the field out it is exactly the same. "red", "car"
    – pdg87
    CommentedMay 16, 2015 at 13:31
  • sorry meant if I change 'LIKE' to 'IN'
    – pdg87
    CommentedMay 16, 2015 at 14:00

1 Answer 1

1

Okay anyone wanting to achieve this, the code works. Just save the values in the custom field without quotes. e.g. red, car

 <?php $list = get_field( "main_attributes" ); $array = explode(',', $list); $args = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'style_atrributes', 'value' => $array, 'compare' => 'IN' ), array( 'key' => 'car_atrributes', 'value' => $array, 'compare' => 'IN' ), ), ); $custom_query = new WP_Query($args); while($custom_query->have_posts()) : $custom_query->the_post(); ?> <div <?php post_class(); ?> id="post-<?php the_ID(); ?>"> <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <?php the_content(); ?> </div> <?php endwhile; ?> <?php wp_reset_postdata(); // reset the query ?> 
2
  • I upvoted. Solving your own question usually deserves that IMHO. Doesn't get_field() return an array already though?
    – s_ha_dum
    CommentedMay 17, 2015 at 16:58
  • Thanks for the upvote. I actually realised this doesn't do what a want either. in my test data I only had one value in 'car_attributes' e.g. 'car' so it worked. But if I add another so: car, red it searches for the exact match and doesnt find it. I guess what I am trying to do is pick out matches from two arrays.. currently looking to see how this is possilble
    – pdg87
    CommentedMay 18, 2015 at 19:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.