Simple Fields
[resolved] Compare fields between posts (2 posts)

  1. sugardaddy
    Posted 3 years ago #

    Actually I use the Custom Field Suite plugin to achieve that.
    But I prefer the flexibility of yours ;-) and the UI in the post admin.
    And I try to achieve what I did with CFS : display a random post in another one by fields comparison. That means I get the posts from a certain category which have a field value equal to the same field of the current post.
    This is actually what I do :

    $product = simple_fields_get_post_value(get_the_id(), array(1, 1), true); // get the field value for "Produit associƩ"
                    global $post;
    				$args = array( 'numberposts' => 1, 'category' => 46, 'meta_query' => array( array( 'key' => 'Produit associƩ', 'value' => $product, 'compare' => '=' ) ), 'orderby' => 'rand' );
    				$usecases = get_posts( $args );
    				foreach( $usecases as $post ) :

    But the combination of key and value doesn't seem to work and, as you advice, I'd like to use IDs instead of names.

    Thanks a lot for your help !


  2. sugardaddy
    Posted 3 years ago #

    I found the solution... and it's a much reliable and easier way to achieve.

    First I get the current value of the concerned field :

    $product = get_post_meta(get_the_id(), "_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0", true);

    Then I create a loop to browse my category and limited to the meta value found in the current post :

    $usecases = new WP_Query( 'cat=46&meta_value='.$product.'&orderby=rand' );

    Then I create my loop :

    while ( $usecases->have_posts() ) : $usecases->the_post();

    And finally, before the endwhile, I need to add a break to get only the first result :

    break; endwhile; wp_reset_postdata();

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Simple Fields
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic