• Resolved Pete

    (@perthmetro)


    I have this piece of code, it’s works great. I use it with CPTs as seen below 'post_type' => mortgage-home-loan. I’m trying to use it on another website which is more or less identical but with a different CPT slug property-for-sale but for some reason the code crashes my page. If I paste in the original mortgage-home-loan slug then it doesn’t crash!

    In my notepad++ something weird happens… the for part of property-for-sale is in a different colour (try it yourself). Which makes me think the for part of the slug is causing the crash… is this a WP bug or my fault?

    original code… no problem

    <?php
    			if ( is_single()) {
    			$categories = get_the_category();
    			if ($categories) {
    			foreach ($categories as $category) {
    			$cat = $category->cat_ID;
    			$args=array(
    			'cat' => $cat,
    			'order' => asc,
    			'post_type' => mortgage-home-loan,
    			//'orderby' => rand,
    			'post__not_in' => array($post->ID),
    			'posts_per_page'=>9999,
    			'caller_get_posts'=>1
    			);
    			$my_query = null;
    			$my_query = new WP_Query($args);
    			if( $my_query->have_posts() ) {
    			// echo '<p>Check out our other '. $category->cat_name . ' products</p>';
    			while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <?php the_title(); ?>			
    
    			 <?php
    			endwhile;
    			}}} wp_reset_query(); }
    			?>

    New code… crashes the page

    <?php
    			if ( is_single()) {
    			$categories = get_the_category();
    			if ($categories) {
    			foreach ($categories as $category) {
    			$cat = $category->cat_ID;
    			$args=array(
    			'cat' => $cat,
    			'order' => asc,
    			'post_type' => property-for-sale,
    			//'orderby' => rand,
    			'post__not_in' => array($post->ID),
    			'posts_per_page'=>9999,
    			'caller_get_posts'=>1
    			);
    			$my_query = null;
    			$my_query = new WP_Query($args);
    			if( $my_query->have_posts() ) {
    			// echo '<p>Check out our other '. $category->cat_name . ' products</p>';
    			while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <?php the_title(); ?>			
    
    			 <?php
    			endwhile;
    			}}} wp_reset_query(); }
    			?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Helen Hou-Sandi

    (@helen)

    Core Lead Developer and 4.0, 4.7, and 5.6 Release Lead

    Strings in PHP (grossly simplified, things that aren’t numbers or special boolean words like true or false) need to be in quotes. That one of your examples works is very likely a coincidence.

    The issure you’re facing here is really just missing quotes.

    If you have error reporting on, you’d see messages like:

    Notice: Use of undefined constant asc - assumed 'asc' in /path/to/file.php on line 11

    In the first example, php is able to successfully make assuptions about the strings you meant, but in the second example the for gets in the way. for is special in php, so when it comes across that in the code it expects certain syntax, and if it’s not there you’ll get an error.

    In PHP, always wrap strings in quotes, either single or double.

    Bare strings are interpreted as constants. When undefined, they are taken literally, so foo becomes ‘foo’. A hyphen is not allowed as part of a variable or constant name, as it’s the minus sign.

    property-for-sale will be interpreted as ( 'property' - 'for' - 'sale' ), which is not a meaningful expression (trying to subtract string from string).

    http://php.net/manual/en/language.types.string.php

    Thread Starter Pete

    (@perthmetro)

    Thanks all

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘CPT slug crashing my template because it has the word 'for' in it?’ is closed to new replies.