Support

Account

Home Forums Front-end Issues filter post object data based on another post object value ID

Solving

filter post object data based on another post object value ID

  • Hi Guys just want to ask how i can filter post object data based on another post object value ID. Do i really need to use JQuery and can anyone point me in the right direction and some sample code.

    I have a post object called mall using post type and another post object called cinema which is listed inside one of my custom fields under mall post type. Can anyone help me out? Im trying to do this inside the front-end

  • Hi @phukol,

    In such a scenario, I would recommend that you use a relationship field to link cinemas in a specific mall.

    If you already have a similar setup, you can then filter the custom posts using the post object id value.

    Here is an article on the same.

    https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    I hope this helps.

  • Thank you. But can you point me in the right direction on how i can use relationship field to link them?

    I already setup my cinema fields which has mall as one of its fields. What i need now is how i can call the cinema’s linked inside the particular mall in my admin menu. I already know how to use wp_query but setting up the fields in admin is quite confusing.

    cinema setup field

    movie setup field

  • Hi @phukol

    In this kind of situation, the solution can be really complicated. Because when you choose the Mall object, it’s not saved in the database yet, so the Cinema doesn’t know the selected Mall ID. In this case, you need to use AJAX to modify the returned Cinema objects. There’s an old tutorial on how to do something similar but for the select field. Please check it out here: https://www.bobz.co/dynamically-populate-select-fields-choice-in-advanced-custom-fields/. I think you can modify it so it works with the post object field too.

    I hope this makes sense 🙂

  • Thank you JAmes, ill try it but i have one more question. How can i call a particular field id from inside the functions.php? we can set the function to a field id by using something like add_filter(‘acf/load_field/key=field_52b1b7007bfa4’ to add that field to the function inside functions.php but what should i do if i want to call a particular field inside one of my function in function.php?

  • Hi there. So far this is my idea. I will use a post object for my mall. So what i want to do is reuse the code and try to check if my post object with id field_576bd813ca393
    to use this jscript:

    `jQuery(document).ready(function($) {

    $( ‘#acf-field_576bd813ca393’ ).change(function () {

    var selected_mall = ”; // Selected value

    // Get selected value
    $( ‘#acf-field_576bd813ca393 option:selected’ ).each(function() {
    selected_mall += $( this ).text();

    });

    }).change();
    });`

    and this code on my functions.php:

    
    function acf_admin_enqueue( $hook ) {
    	 
      $type = get_post_type(); // Check current post type
      $types = array( 'post' ); // Allowed post types
     
      if( !in_array( $type, $types ) )
    	  return; // Only applies to post types in array
      
      wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri().'/autopopulate.js' ); // '/assets/js/
     
      wp_localize_script( 'populate-area', 'pa_vars', array(
    		'pa_nonce' => wp_create_nonce( 'pa_nonce' ), // Create nonce which we later will use to verify AJAX request
    	  )
      );
    }
    	 
    		add_action( 'admin_enqueue_scripts', 'acf_admin_enqueue' );

    but my problems are the ff:

    1.) i tried using console.log and document.write to try to log the value of selected_mall but i cant seem to make it display on my console and admin page.

    2.) inside $( ‘#acf-field_576bd813ca393’ ).change(function (), how can i check all the cinemas i had also placed from a previous acf custom field? i know i need to use wp_query right and loop through all data. but if i am to use a post object which has both id and value how can i do it?

  • Hi @phukol

    Because this is an advanced topic, I’m afraid I can’t help you much. If you need help, you can always hire a developer to help you out with it, and I’d recommend looking for one on https://studio.envato.com/ or https://www.upwork.com/. I’m sorry about that.

    Regarding you question, I believe you can get the value like this:

    selected_mall = $('#acf-field_576bd813ca393-input').val();

    You need to modify the code in Step 4. Function to get areas by country to get the options. Please keep in mind that the post object field is different from the select field. You need to check the page source to see how it works.

    I hope this makes sense 🙂

  • I know this is kinda late but perhaps could help others seeking to solve similar problems. I managed to do this by registering a transient value in the AJAX call which is immediately available to filter, populate, and correctly save post object selections.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘filter post object data based on another post object value ID’ is closed to new replies.