• Resolved 0rca

    (@0rca)


    I’ve created a post type “Orderlist” and assigned a custom taxonomy “Objectrights” to it. I extended users with a relationship field to “Objectrights”.

    A user can have several “Objectrights”, an “Orderlist” has only one “Objectright”.

    I am now trying to list all “Orderlists” that have the same “Objectrights” as the currently logged in user.

    This is almost working like this:

    [pods name=”orderlist” where=”objectrights.term_id = {@user.objektrights}” template=”Orderlist”]

    The problem is, it will only list the first occurrence. For example the user has the objectrights 1, 2 and 3 and 3 Orderlists each have one of those objectrights assigned. I would expect the shortcode to list all three, but it only shows the first one (whatever objectright I have sorted to the top in the users profile). I have also tried:

    [pods name=”orderlist” where=”objectrights.term_id IN ({@user.objektrights})” template=”Orderlist”]

    with the same result ??

    Can aynone point me in the right direction? Or is it impossible?

Viewing 9 replies - 16 through 24 (of 24 total)
  • Plugin Support Paul Clark

    (@pdclark)

    It’s not really possible for me to verify without looking at your data structure. But generally, I don’t think it’s possible to have a field name and taxonomy name of the same key. Please look at the field you’ve defined on the user object and verify that you are using that field name when referencing {@user.whatever}

    Thread Starter 0rca

    (@0rca)

    This is what I have done:

    I created the CPT “Orderliste” with a few simple fields.

    Then I created a taxonomy “Objektrechte” and enabled as connection to post type “Orderlisten”
    I added a field “User” to it:
    Field Type: Relationhip
    Related Type: User
    Bi-directional Field: Objektrechte (objektrechte) [Pod: user]

    This is the extension of “Users”
    Field Type: Relationhip
    Related Type: Objektrechte (objektrechte)
    Bi-directional Field: User (user) [Pod: objektrechte]

    Plugin Support Paul Clark

    (@pdclark)

    Please share a Pods package export of your exact data structure using Pods > Components > Import / Export Packages.

    For example, see https://github.com/pods-framework/pods/issues/7116#issuecomment-1625173063 — if you have a taxonomy of key objektrechte and then attempt to create a field of key objektrechte, Pods will display an error and refuse to do it. So I still suspect that your relationship field on user is not named objektrechte. It’s related to taxonomy objektrechte, but the field name is likely something else.

    Plugin Support Paul Clark

    (@pdclark)

    If that truly is the field name, it may be that it’s allowed on a user, but not a post type. All the same, it would be immensely helpful to have the Pods Package export to test an actual shortcode and template or suggest an alternative approach in PHP. Without that, it would be presumptuous to make further guesses at to what may or may not be happening in relationship to an unclear data structure.

    Thread Starter 0rca

    (@0rca)

    Thread Starter 0rca

    (@0rca)

    And as to why: I am simply trying to list only the “Orderlisten” assigned to the currently logged in user.

    But I can’t assign users directly to Orderlisten, so I have created the taxonomy “Objektrechte”. An Orderlist has one Objektrecht assigned, a user can have many Objektrechte and mutliple users can have the same Objektrechte.

    This is essentially to achieve a kind of rights-mangement, so that only assigned users can see “their” objects (buildings) and the corresponding Orderlisten (list of products to be ordered).

    Plugin Support Paul Clark

    (@pdclark)

    Great, got it, thanks.

    I will run some tests in get back later today with some possible solutions for that scenario.

    It’s almost 4am here now and has been a long day, so I’ll need to rest a bit before importing and attempting queries that would result in listing content and managing access per user.

    Thread Starter 0rca

    (@0rca)

    Oh, another night owl ?? Sleep well then ??

    Plugin Support Paul Clark

    (@pdclark)

    Thanks for your patience @0rca .

    The code below should resolve all the issues.

    There were three things:

    • When querying a multi-select taxonomy relationship field using {@user}, the name being the same was fine, but the values output and passed to helpers are not correct. This issue has been documented for resolution here: https://github.com/pods-framework/pods/issues/7150
    • When desiring to pass IDs to a shortcode via magic tags, a helper function is necessary for two reasons: 1. Multiple IDs will will contain “and” in the comma-separated string. 2. Shortcodes are not re-processed after magic tags are processed, so the shortcode with magic tag values will not parse.

    The actions to take are:

    • Install the code for the shortcode and helper function below.
    • Where there was previously the [pods] shortcode similar to [pods name="orderlist" where="objectrights.term_id IN ({@user.objektrights})" template="Orderlist"], instead use [pods-orders-for-user]
    • In the template Orderliste, where there was previously [woocommerce_quick_order_table taxonomy="objektrechte" categories="{@objektrechte.term_id}" order="DESC" orderby="menu_order"] instead use {@objektrechte.term_id,term_ids_to_quick_order_table} . I was not able to test the output of [woocommerce_quick_order_table], as it’s a paid plugin, but I was able to verify the correct attributes are being passed with a comma-separated list of term IDs.
    • If the shortcodes output literally, make sure define( 'PODS_SHORTCODE_ALLOW_SUB_SHORTCODES', true ); is set in wp-config.php
    • It should be noted that if no objektrechte terms are assigned to the current orderliste post object, [woocommerce_quick_order_table] will not be called and Keine Bestellungen gefunden. will instead be output.
    <?php
    /**
     * Get term_ids from the current user relationship field "objektrechte".
     * Query all "orderliste" posts associated with the associated terms.
     * Display Pods Template "Orderliste".
     * 
     * Usage:
     *     [pods-orders-for-user]
     */
    add_shortcode(
    	'pods-orders-for-user',
    	function( $atts, $content, $tagname ){
    		$user_objektrechte = get_user_meta( get_current_user_id(), 'objektrechte', false );
    		if ( is_array( $user_objektrechte ) ) {
    			$user_objektrechte = implode( ',', $user_objektrechte );
    		}else {
    			$user_objektrechte = 0;
    		}
    
    		$orderliste = pods(
    			'orderliste',
    			[
    				'limit' => -1,
    				'where' => sprintf( 'objektrechte.term_id IN( %s )', $user_objektrechte ),
    			]
    		);
    
    		return $orderliste->template( 'Orderliste' );
    	}
    );
    
    /**
     * Convert an array of term_ids from a Pods Magic Tag to a comma-separated string of IDs.
     * Pass the IDs as categories to the [woocommerce_quick_order_table] shortcode.
     * 
     * Usage in a Pods Template:
     *     {@objektrechte.term_id,term_ids_to_quick_order_table}
     * 
     * Where "objektrechte" is the taxonomy name associated with the currently viewed post type.
     */
    function term_ids_to_quick_order_table( $term_ids ) {
    	if ( empty( $term_ids ) ) {
    		return '<p>Keine Bestellungen gefunden.</p>';
    	}
    	$term_ids = implode( ',', (array) $term_ids );
    
    	return do_shortcode(
    		sprintf(
    			'[woocommerce_quick_order_table taxonomy="objektrechte" categories="%s" order="DESC" orderby="menu_order"]',
    			esc_attr( $term_ids )
    		)
    	);
    }
Viewing 9 replies - 16 through 24 (of 24 total)
  • The topic ‘Compare CPT taxonomy to user’s relationship field’ is closed to new replies.