Forum Replies Created

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter luknijdwarazy

    (@luknijdwarazy)

    Thank you so much for explaining this to me in details, now I understand how to build permalink structure on my site pages to be more clear for bots. 🙂

    Thread Starter luknijdwarazy

    (@luknijdwarazy)

    Eureca! 🙂 I have manage to list for example only new colour copy devices, without listing for example used colour copy devices.

    But I must say the code is different and very light. I just used rigth relation and operator with my taxonomy.

    Thx for helping me eliminate unknown.

    Thread Starter luknijdwarazy

    (@luknijdwarazy)

    I must admit, that monologue was quite creative. I answered all my question. Few arrays was made because echo was inside the loop 🙂 bravo me!

    But serious, to get the array of specific id’s I had to add if statement:

    
    if ($term->term_id == numeric )
    

    The result is:

    
    Array
    (
        [0] => 12
        [1] => 12
        [2] => 12
    )
    

    But how to combine this with my $categories loop???????? -_-! How to display all post which has the term_id 12???

    Thread Starter luknijdwarazy

    (@luknijdwarazy)

    Sorry to tire you so much! 🙂 I know it’s not easy to repair someone code specialy if you dnt’t have access to source code.

    Sooo I have create 3 (custom) posts with terms ‘new’ and ‘bw’. If I do somethink like this:

    
      $args = array( 'post_type' => 'devices');
      $loop = new WP_Query( $args );
    
      while ( $loop->have_posts() ) : $loop->the_post();
        $terms = get_the_terms(  get_the_id(), 'copy_device');
    
      foreach ( $terms as $term ) {
          $termID[] = $term->term_id;
      }
    
        echo '<pre>'; print_r( array_values($termID) ); echo '</pre>';
    
      endwhile;
    

    The output is:

    Array
    (
    [0] => 12
    [1] => 9
    )
    Array
    (
    [0] => 12
    [1] => 9
    [2] => 12
    [3] => 9
    )
    Array
    (
    [0] => 12
    [1] => 9
    [2] => 12
    [3] => 9
    [4] => 12
    [5] => 9
    )

    So 12 = bw and 9 = new. Now if I do echo $termID[0] I get below output:

    121212

    three bw copy devices, right? But if I add color device as a fourth device and I do echo I get somethink like this:

    11111111

    all arrays look like this:

    Array
    (
    [0] => 11
    [1] => 9
    )
    Array
    (
    [0] => 11
    [1] => 9
    [2] => 12
    [3] => 9
    )
    Array
    (
    [0] => 11
    [1] => 9
    [2] => 12
    [3] => 9
    [4] => 12
    [5] => 9
    )
    Array
    (
    [0] => 11
    [1] => 9
    [2] => 12
    [3] => 9
    [4] => 12
    [5] => 9
    [6] => 12
    [7] => 9
    )

    Do you know why this is working like that? And how to gather for example only bw devices?

    • This reply was modified 5 years, 9 months ago by luknijdwarazy.
    Thread Starter luknijdwarazy

    (@luknijdwarazy)

    I did the following:

    
    <?php
      $terms = get_terms([
        'taxonomy' => 'copy_device',
        'hide_empty' => false,
      ]);
    
      echo '<pre>'; print_r(array_values($terms)); echo '</pre>';
    ?>
    

    The result is:

    Array
    (
    [0] => WP_Term Object
    (
    [term_id] => 12
    [name] => BW
    [slug] => bw
    [term_group] => 0
    [term_taxonomy_id] => 12
    [taxonomy] => copy_device
    [description] =>
    [parent] => 9
    [count] => 1
    [filter] => raw
    )

    [1] => WP_Term Object
    (
    [term_id] => 11
    [name] => Colour
    [slug] => colour
    [term_group] => 0
    [term_taxonomy_id] => 11
    [taxonomy] => copy_device
    [description] =>
    [parent] => 9
    [count] => 1
    [filter] => raw
    )

    [2] => WP_Term Object
    (
    [term_id] => 9
    [name] => New
    [slug] => new
    [term_group] => 0
    [term_taxonomy_id] => 9
    [taxonomy] => copy_device
    [description] =>
    [parent] => 0
    [count] => 2
    [filter] => raw
    )

    [3] => WP_Term Object
    (
    [term_id] => 10
    [name] => Used
    [slug] => used
    [term_group] => 0
    [term_taxonomy_id] => 10
    [taxonomy] => copy_device
    [description] =>
    [parent] => 0
    [count] => 1
    [filter] => raw
    )
    )

    so why is it not working with the foreach loop?

    Thread Starter luknijdwarazy

    (@luknijdwarazy)

    Hi, sorry for long response but I couldn’t do it earlier. So I try to do this from the beginning.

    I installed fresh WP with twenty twenty theme. I registered in function.php, custom post type ‘devices’ and taxonomy ‘copy_devices’ with children’s ‘new’, ‘used’, ‘bw’ and ‘colour’.

    copy_device
    -new
    –bw
    –colour

    -used
    (..)

    My function.php looks like this:

    
    // custom post type
    function td_devices_posttype() {
     $labels = array(
    	 'name'                => _x( 'Devices', 'Post Type General Name', 'textdomain' ),
    	 'singular_name'       => _x( 'Device', 'Post Type Singular Name', 'textdomain' ),
    	 'menu_name'           => esc_html__( 'Devices', 'textdomain' ),
    	 'parent_item_colon'   => esc_html__( 'Parent Device', 'textdomain' ),
    	 'all_items'           => esc_html__( 'All Devices', 'textdomain' ),
    	 'view_item'           => esc_html__( 'View Devices', 'textdomain' ),
    	 'add_new_item'        => esc_html__( 'Add New Devices', 'textdomain' ),
    	 'add_new'             => esc_html__( 'Add New', 'textdomain' ),
    	 'edit_item'           => esc_html__( 'Edit Devices', 'textdomain' ),
    	 'update_item'         => esc_html__( 'Update Devices', 'textdomain' ),
    	 'search_items'        => esc_html__( 'Search Devices', 'textdomain' ),
    	 'not_found'           => esc_html__( 'Not found', 'textdomain' ),
    	 'not_found_in_trash'  => esc_html__( 'Not found in Trash', 'textdomain' )
     );
     $args = array(
    	 'label'               => esc_html__( 'devices', 'textdomain' ),
    	 'description'         => esc_html__( 'All devices you have', 'textdomain' ),
    	 'labels'              => $labels,
    	 'taxonomies'          => array( 'category', 'post_tag'),
    	 'hierarchical'        => true,
    	 'public'              => true,
    	 'show_ui'             => true,
    	 'show_in_menu'        => true,
    	 'show_in_nav_menus'   => true,
    	 'show_in_admin_bar'   => true,
    	 'menu_position'       => 100,
    	 'can_export'          => true,
    	 'has_archive'         => esc_html__( 'devices' ),
    	 'exclude_from_search' => false,
    	 'publicly_queryable'  => true,
    	 'query_var' 	       => true,
    	 'show_admin_column'   => true,
    	 'capability_type'     => 'post',
    	 'rewrite'             => array('slug' => 'devices'),
    	 'supports'            => array( 'title','editor','thumbnail', 'custom-fields')
     );
     register_post_type( 'devices', $args );
    }
    add_action( 'init', 'td_devices_posttype' );
    
    // custom taxonomies Copy devices
    function td_posttype_taxonomy() {
    	$labels = array(
    		'name' 			=> 'Copy Devices',
    		'singular_name'		=> 'Copy Device',
    		'search_items'		=> 'Search items',
    		'all_items'		=> 'All items',
    		'parent_item'		=> 'Parent item',
    		'parent_item_colon'	=> 'Parent item colon:',
    		'edit_item'		=> 'Edit item',
    		'update_item'		=> 'Update item',
    		'add_new_item'		=> 'Add new item',
    		'new_item_name'		=> 'New item name',
    		'menu_name'		=> 'Copy Devices'
    	);
    
    	$args = array (
    		'hierarchical' 		=> true,
    		'labels'		=> $labels,
    		'show_ui'		=> true,
    		'show_admin_column'	=> true,
    		'show_in_rest'		=> true,
    		'query_var'		=> true,
    		'rewrite'		=> array( 'slug' => 'devices/copy-devicex')
    	);
    
    	register_taxonomy( 'copy_device', array( 'devices' ), $args );
    }
    add_action('init', 'td_posttype_taxonomy');
    

    My WP page looks like this:

    
    <?php
                // get all devices
                $categories = get_terms(
                  array(
                    'taxonomy'    => esc_html__('copy_device'),
                    'hide_empty'  => true,
                  )
                );
    
                // trying to hook terms IDs (Not working?)
                $termID = array();
                $terms = get_the_terms($post->ID, 'copy_device');
    
                foreach ($terms as $term) {
                  $termID[] = $term->term_id;
                }
    
                echo '<pre>'; print_r(array_values($termID)); echo '</pre>';
    
                // Loop and return terms
                foreach ( $categories as $categorie ):
    
                    // set up a new query
                    $args = new WP_Query(
                        array(
                            'post_type' => 'devices',
                            'tax_query' => array(
                                array(
                                    'taxonomy'  	=> 'copy_device',
                                    'terms'     	=> array( 'new', 'colour' ),
                                    'field'     	=> 'slug',
                                    'hide_empty' 	=> false,                                
                                )
                            )
                        )
                    );
                ?>
    
                <h3><?php echo $categorie->name; ?></h3>
                <ul>
                <?php while ($args->have_posts()) : $args->the_post(); ?>
                    <li><?php the_title(); ?></li>
                <?php endwhile; ?>
                </ul>
    
                <?php
                    $args = null;
                    wp_reset_postdata();
                  endforeach;
                ?>
    

    The result is “Warning: Invalid argument supplied for foreach() in /(…)/wp-content/themes/twentytwenty/page.php on line 32”

    Array
    (
    )

    The line 32 is “foreach ($terms as $term) {”

    I didn’t tried to repair this CSS situation now because it has lower priority. I will try to not change the code so it will be easier for you to help me.

    Thread Starter luknijdwarazy

    (@luknijdwarazy)

    Hi,

    now I get: Warning: Invalid argument supplied for foreach() in “URL to file.php” on line 26. The line 26 looks like “foreach ( $terms as $term ){”

    
    22
    23 $termID = [];
    24 $terms = get_the_terms( get_the_ID(), 'kopiarka' );
    25
    26	foreach ( $terms as $term ){
    27		$termID[] =	$term->term_id;
    28	}
    29	
    30	echo '<pre>2 ';
    31	print_r( array_values( $termID ) );
    32	echo '</pre>';
    33
    

    This is so problematic 🙁

    Thread Starter luknijdwarazy

    (@luknijdwarazy)

    Well, backing to the loop case. I did something like this:

    
    $termID = [];
    $terms = get_the_terms( get_the_ID(), 'device');
    
    	foreach ( $terms as $term ){
    		$termID[] = $term->ID;
    	}
    	echo '<pre>'; print_r( array_values($termID) ); echo '</pre>';
    

    end I get an erron:

    Notice: Trying to get property ‘ID’ of non-object in C:\xampp\htdocs\wordpress\wp-content\themes\theme-name\template-parts\content-offer.php on line 23

    the result is:

    Array
    (
    [0] =>
    [1] =>
    )

    • This reply was modified 5 years, 10 months ago by luknijdwarazy.
    • This reply was modified 5 years, 10 months ago by luknijdwarazy.
    Thread Starter luknijdwarazy

    (@luknijdwarazy)

    Hi, I appreciate you responde for my case. I will try to respond in smaller pieces. Yes, I altered the labels of taxonomy but you have right it should be copy-devices.

    So, I started with checking hierarchy and the collision problems. First I update the WordPress to 5.4.2. Next, I deleted all categories in custom post type taxonomie and created new ones with new names to avoid collision. I did the same hierarchy as before the results are unexpected. I can see hierarchy in taxonomy panel, but if I edit the post the hierarchy disapear, look…

    https://ibb.co/LQ8PNjb
    https://ibb.co/QN4szBw

    I installed Custom Post Type UI plugin to check if the problem with hierarchy still exist and its not exist. The hieratchy stay on its place. I`m using the _s theme generated with sass. But it doesn’t matter what theme Im using the problem occurs on everyone. I also disactivate all plugins and create new categories in taxonomie and the problem still exist.

    You said “I cannot replicate the alteration of hierarchy upon saving a post.” What dose it mean? Can this situation be fixed?

    I added the 'show_in_rest' => true, to the taxonomy registration. I really dont know what to do with this hierarchy problem now… 🙁

    Your response helps me a lot, thanks! I will try to fix the loop situation and not to think about hierarchy until tomorrow.

    • This reply was modified 5 years, 10 months ago by luknijdwarazy.
    Thread Starter luknijdwarazy

    (@luknijdwarazy)

    Hey joy,

    but maybe this is normal? Dont want to waste your time becouse I got minus for that question on another forum 🙁

    Im using WordPress 5.3.4. Im using default editor https://ibb.co/6rqhpwb. Yes, change is happening on Save and it`s pernament till I uncheck selected categories and Save. Then hierarchy is again normal.
    I registered custom post type and taxonomy by coding them by myself. This is my first contact with the CPT so I dont know exacly how to achive the hierarhy below. The funcions are little bit diffrent now because Im trying to do it right. But I am not sure I`m doing this right.

    It is posible to have one custom post type for example Device and have taxonomy something like this?

    1. Devices
    1.1 Copy Devices
    1.1.1 New
    A. Color
    B. W/B
    1.1.2 Used
    A. Color
    B. W/B
    2.1 Print Devices
    2.1.1 New
    A. Color
    B. W/B
    2.1.2 Used
    A. Color
    B. W/B
    3.1 Other Devices
    3.1.1 New
    A. Color
    B. W/B
    3.1.2 Used
    A. Color
    B. W/B

    // function.php
     // custom post type
     function td_devices_posttype() {
      $labels = array(
        'name'                => _x( 'Devices', 'Post Type General Name', 'textdomain' ),
        'singular_name'       => _x( 'Device', 'Post Type Singular Name', 'textdomain' ),
        'menu_name'           => esc_html__( 'Devices', 'textdomain' ),
        'parent_item_colon'   => esc_html__( 'Parent Device', 'textdomain' ),
        'all_items'           => esc_html__( 'All Devices', 'textdomain' ),
        'view_item'           => esc_html__( 'View Devices', 'textdomain' ),
        'add_new_item'        => esc_html__( 'Add New Devices', 'textdomain' ),
        'add_new'             => esc_html__( 'Add New', 'textdomain' ),
        'edit_item'           => esc_html__( 'Edit Devices', 'textdomain' ),
        'update_item'         => esc_html__( 'Update Devices', 'textdomain' ),
        'search_items'        => esc_html__( 'Search Devices', 'textdomain' ),
        'not_found'           => esc_html__( 'Not found', 'textdomain' ),
        'not_found_in_trash'  => esc_html__( 'Not found in Trash', 'textdomain' ),
      );
      $args = array(
        'label'               => esc_html__( 'devices', 'textdomain' ),
        'description'         => esc_html__( 'All devices you have', 'textdomain' ),
        'labels'              => $labels,
        'taxonomies'          => array( 'copy_device'),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 100,
        'can_export'          => true,
        'has_archive'         => esc_html__( 'devices' ),
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'query_var' 	  => true,
        'show_admin_column'   => true,
        'capability_type'     => 'post',
        'rewrite'             => array('slug' => 'devices'),
        'supports'            => array( 'title','editor','thumbnail', 'custom-fields'),
      );
      register_post_type( 'devices', $args );
     }
     add_action( 'init', 'td_devices_posttype', 0 );
    
     // custom taxonomies Copy devices
     function td_posttype_taxonomy() {
    	 $labels = array(
    		 'name' 		=> 'Copy Devices',
    		 'singular_name'	=> 'Copy Device',
    		 'search_items'		=> 'Search items',
    		 'all_items'		=> 'All items',
    		 'parent_item'		=> 'Parent item',
    		 'parent_item_colon'	=> 'Parent item colon:',
    		 'edit_item'		=> 'Edit item',
    		 'update_item'		=> 'Update item',
    		 'add_new_item'		=> 'Add new item',
    		 'new_item_name'	=> 'New item name',
    		 'menu_name'		=> 'Copy Devices'
    	 );
    
    	 $args = array (
    		 'hierarchical' 	=> true,
    		 'labels'		=> $labels,
    		 'show_ui'		=> true,
    		 'show_admin_column'	=> true,
    		 'query_var'		=> true,
    		 'rewrite'		=> array( 'slug' => 'devices/copy-device'),
    	 );
    
    	 register_taxonomy( 'copy-device', array( 'devices' ), $args );
    
    	 // taxonomy colors
    	 $labels = array(
    		 'name' 		=> 'Colours',
    		 'singular_name'	=> 'Colour',
    		 'search_items'		=> 'Search items',
    		 'all_items'		=> 'All items',
    		 'parent_item'		=> 'Parent item',
    		 'parent_item_colon'	=> 'Parent item colon:',
    		 'edit_item'		=> 'Edit item',
    		 'update_item'		=> 'Update item',
    		 'add_new_item'		=> 'Add new item',
    		 'new_item_name'	=> 'New item name',
    		 'menu_name'		=> 'Print Devices'
    	 );
    	 $args = array (
    		 'hierarchical' 	=> false,
    		 'labels'		=> $labels,
    		 'show_ui'		=> true,
    		 'show_admin_column'	=> true,
    		 'query_var'		=> true,
    		 'rewrite'		=> array( 'slug' => 'print-device'),
    	 );
     }
     add_action('init', 'td_posttype_taxonomy');

    I have problems with the loop also if i looping the CPT by taxonomy and the terms I wan`t to for example get all “new color copy devices” but not “used color copy devices” but the output I get is every device which has “color” or “new” so I get also “used color copy devices” because it has the “color” category.

    The loop looks like this:

    <?php
          // get all devices
          $categories = get_terms(
            array(
              'taxonomy'    => esc_html__('copy-device'),
              'hide_empty'  => true,
            )
          );
    
          // Loop and return terms
          foreach ( $categories as $categorie ):
    
              // set up a new query
              $args = new WP_Query(
                  array(
                      'post_type' => 'devices',
                      'tax_query' => array(
    											'relation'	=> 'AND',
                          array(
                              'taxonomy'  		=> 'copy-device',
                              'terms'     		=> array( 'new' ),
                              'field'     		=> 'slug',
                              'hide_empty' 		=> false,
                          )
                      )
                  )
              );
          ?>
    
    		  <h3><?php echo $categorie->name; ?></h3>
    		  <ul>
    		  <?php while ($args->have_posts()) : $args->the_post(); ?>
    		      <li><?php the_title(); ?></li>
    		  <?php endwhile; ?>
    		  </ul>
    
    		  <?php
    		      $args = null;
    		      wp_reset_postdata();
    		    endforeach;
    		  ?>

    In ftont end I get every device couple of times

    https://ibb.co/bR8ccyy

    Could you help me with some of this problems? Any help is valuable. This is my third day i`m trying to set this right.

    • This reply was modified 5 years, 10 months ago by luknijdwarazy.
    • This reply was modified 5 years, 10 months ago by luknijdwarazy.
    • This reply was modified 5 years, 10 months ago by luknijdwarazy.
    • This reply was modified 5 years, 10 months ago by luknijdwarazy.
    • This reply was modified 5 years, 10 months ago by luknijdwarazy.
Viewing 10 replies - 1 through 10 (of 10 total)