• Resolved jordiwordpress

    (@jordiwordpress)


    I have a pod of books called “book” and another of people who have books called “item”. I put the form shortcode for “item” in the “book” template and from there I create a new “item” post and relate them.
    But there are several books with the same name, and I can’t know which of the books with the same name I should choose. I read that in the “Display Field” in Selection List of the “relationship options” it only supports a single field. Since the form shortcode is in the “book” template, is there any way for the display list to only show the book of the post that is currently displayed?

    Thanks a lot

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Paul Clark

    (@pdclark)

    is there any way for the display list to only show the book of the post that is currently displayed?

    Because there is a relationship between the item and book, I assume what you mean you want the item form to always have the current book selected with no other options.

    The below code assumes the name of the relationship field on item is book. Since the current book will be selected, there is also an action that hides that form entry.

    If displaying the form with an alternative label for the book is preferable, one would remove the action on wp_head and instead modify $label with a new label for the book.

    It’s worth noting that this will create a new item related to the book for every form entry.

    // Only run this on the frontend.
    add_action(
    	'template_redirect',
    	function(){
    		/**
    		 * Filter the data of the Items form to only display display the current book.
    		 * 
    		 * @see https://github.com/pods-framework/pods/blob/a20d6392eea3cc1fa181953e215cc1c262b15100/classes/fields/pick.php#L2117
    		 * @see https://github.com/pods-framework/pods/blob/a20d6392eea3cc1fa181953e215cc1c262b15100/classes/fields/pick.php#L3110
    		 */
    		add_filter(
    			'pods_field_pick_data',
    			function( $data, $name, $value, $options, $pod, $id ) {
    				// Only apply this to the "book" field.
    				if ( 'pods_field_book' !== $name ) {
    					return $data;
    				}
    
    				$newdata = [];
    				foreach( $data as $id => $label ) {
    					if ( $id === get_the_ID() ) {
    						// Create a new array which only contains the current book.
    						// If an alternate label is desired, that could be edited here.
    						$newdata[ $id ] = $label;
    						break;
    					}
    				}
    				return $newdata;
    			},
    			10,
    			6
    		);
    
    		// Hide the Book field on "book" posts.
    		if ( is_singular( 'book' ) ) {
    			add_action(
    				'wp_head',
    				function(){
    					?>
    					<style>
    						label.pods-form-ui-label-pods-field-book, #pods-form-ui-pods-field-book { display:none; }
    					</style>
    					<?php
    				}
    			);
    		}
    	}
    );
    • This reply was modified 1 year, 3 months ago by Paul Clark.
    Thread Starter jordiwordpress

    (@jordiwordpress)

    Thanks Paul,

    I had copy the code in functions.php and the result is:

    1. The list of “book” field don’t display any book on the form. ( that’s ok)

    2. The current book of the post don’t be displayed.
    if I “click” on the label for safe the new “item” post, no book is related in the new post.

    thanks

    Plugin Support Paul Clark

    (@pdclark)

    Did you check that your names of the post types and fields match?

    did you note that the second function hides the field from the form?

    Thread Starter jordiwordpress

    (@jordiwordpress)


    did you note that the second function hides the field from the form?

    I see, I’ve checked with other fields of the form and it’s work. I think this is so because the name of the field that you call “book” contains ”_” . I call “borrowed_book”

    label.pods-form-ui-label-pods-field-borrowed_book
    Plugin Support Paul Clark

    (@pdclark)

    Yes so then you will need to change

    pods_field_book

    To

    pods_field_borrowed_book

    …in the first function.

    As noted in the earlier post, field names were not specified in the original post, so I assumed the field name was “book”.

    Please read the code critically to come to an understanding of how it works to adapt it to your needs. If you have further issues, please be specific about the exact configuration and desired output.

    Thread Starter jordiwordpress

    (@jordiwordpress)

    Yes, I understand that you are saying. And I have changed “book” by “borrowed_book”, of course.

    What I’m saying is that the second function only hides the field from the form if the field not contains “_”

    p.e.
    label.pods-form-ui-label-pods-field-borrowed_book, #pods-form-ui-pods-field-borrowed_book { display:none; }

    Doesn’t hide

    label.pods-form-ui-label-pods-field-affiliate, #pods-form-ui-pods-field-afilliate { display:none; }

    Hides

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @jordiwordpress

    Then please check the page code to see the actual CSS classes.

    My first guess would be that underscores are replaced by dashes so it should be borrowed-book.

    Cheers, Jory

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Possibily of display list only the name of current post’ is closed to new replies.