• Hi, this is probably a really dumb question but I’m pretty new to all this and just can’t work it out. I have created this code.

    $my_title = wp_title('', false);
    $data = mysql_query("SELECT * FROM wp_posts WHERE post_title='$my_title' AND post_mime_type='image/jpeg'")
    or die(mysql_error());

    Basically it’s to tell a carousel to pull images whose names match the title of the page from a database. When I echo the “$my_title” variable it works and when I substitue “wp_title(”, false);” for a single word like ‘products’ it also works so I’m quite confused. Is it something to do with Global variables (that’s my stab in the dark)?

    Thanks for your help in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • when you put $my_title in single quotes in the string, PHP is no longer interpreting it as a variable and processes it as a string with contents of ‘$my_title’;

    this should work:

    $sql = "SELECT * FROM wp_posts WHERE post_title='" . $my_title . "' AND post_mime_type='image/jpeg'";
    $data = mysql_query($sql);

    A way you can debug such tricky stuff is
    echo $sql;
    which shows you the query that is being sent to MySQL. You can paste that output from your browser window into the phpMyAdmin sql query window. If the query won’t run without error in phpMyAdmin, it won’t work in your program.

    Thread Starter chatotek

    (@chatotek)

    Thanks so much, the idea to echo the $sql part was genius! Just in case anyone’s reading this the only problem I had then was trimming the white space that was appearing in front of the $mytitle when the variable was generated. I added

    function af_titledespacer($title) {
    	return trim($title);
    }
    
    add_filter('wp_title', 'af_titledespacer');

    to the functions file and that solved the problem.
    Thanks again!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_title as SQL variable’ is closed to new replies.