Support » Themes and Templates » Issue with custom PHP query

Issue with custom PHP query

  • Okay, so I’m working on http://www.jamespickettphotography.com which is using the Black Canvas StudioPress theme.

    At the bottom of the home page and individual post pages, there’s a “recent” section that shows thumbnails from all the recent posts.

    What I’d like to do is have it so that it only showed thumbnails from the recent posts inside the category that the current post is in. I don’t mind the running history on the home page, but for example, when someone’s in a post in “what she left”, I’d like for the bottom “recent” section to only show the thumbnails for posts listed in the “what she left” category, and nothing else.

    The issue I think comes in where I don’t want to have to create a new template every time he adds a new category if I don’t have to. Is there a way to use parent/child/if/something statements ONE time so that I don’t have to create a new file every time he calls me up to tell me he started a new category?? I’m just at a loss for sorting this out right now.

    How complicated can this be? I know I’ve probably got the solution buried inside my head somewhere, but I can’t seem to dig it out right now, and would appreciate the help immensely.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi ladydelaluna,

    I’ve got an idea for this.

    You could use a conditional (IF statement) to assist you with this. It could look something like this:

    if ( is_front_page () )
    else if ( in_array ( $category_id, $categories ) )

    In the above example, the $category_id variable is the id of the category currently being viewed and the $categories variable holds an array of all the categories the current post is listed under. The $category_id variable could be acquired at random from the array or passed through the page. There are various methods in the WordPress Codex on how to acquire the category id. The in_array() function checks whether a variable is listed in the array passed to it… in this case, $categories.

    Although this example is a tad rough and rather simple, I hope it helps. 🙂

    Hi mattyza – Ummm I can try that, but the problem is I’m not all that proficient in PHP to begin with, so I was hoping to find someone who knew the theme or could figure out what it is… (cause it’s not a slider… lol)

    But thank you very much for taking the time to try – I suppose I can see if I can play around somehow, but I’m still hoping for someone to be able to hand me the code I need I guess. lol Thank you!

    Hi Ladydelaluna. 🙂

    No problem. 🙂 I’ll try looking around a bit and see if I can find anything to help. 🙂

    Thanks tons, mattyza! I appreciate the efforts SO much!

    Hi ladydelaluna.

    Just popping in to let you know I’m working on this. I’m not familiar with the Studiopress theme, although I do believe I’m coming to a solution. Just have to test a few WP functions to make sure I’m on the right path with what I’m thinking for a solution. 🙂

    Hi ladydelaluna.

    As mentioned above, I’m not familiar with the construction of the Studiopress WordPress theme. When reading your original post above, the process looks, to me, like this:

    Goal: Display images of other posts in the same category(s) as the post currently being viewed.

    And the process:

    1. If the page being viewed isn’t a single post page, display the images as they are currently.

    2. If the page being viewed is a single post page, do the following:

    2.1 Determine the IDs of the categories the current post is categorised under.

    2.2 Use a custom SQL query to pull the values of all other posts in hte same categories into an array.

    2.3 If the query returns no results, display the images as they are on the homepage.

    2.4 Otherwise, if the query returns results, loop through and display the images within the results only.

    Using the first snippet of code above, we can modify it to read as follows:

    if ( is_single() ) {
    } else {

    Knowing this, we’ve solved step 1.

    Step 2 is clear, once the code is in place. The first part looks, in my mind, like this:

    	$categories = get_the_category();
    	$category_ids = array();
    	foreach ($categories as $category) {
    		$category_ids[] = $category->cat_ID;
    	global $wpdb;
    	$sql = "SELECT
    				ID, post_title
    			" . $wpdb->prefix . "posts
    			" . $wpdb->prefix . "term_relationships ON ID = object_id
    				post_type = 'post' AND post_status = 'publish' AND ID != " . $post->ID . "
    	for ($i = 0; $i < count($category_ids); $i++) {
    		$sql .= " term_taxonomy_id = " . $category_ids[$i];
    		if ($i < count($category_ids)-1) {
    			$sql .= " OR ";
    	$sql.= ") GROUP BY ID ORDER BY post_date ASC";
    	$rs = $wpdb->get_results($sql, ARRAY_A);

    Essentially, the above code handles steps 2.1 and 2.2. Steps 2.3 and 2.4 are a matter of running an IF statement that looks something like this:

    <?php if (empty($rs)) {
    } else {

    As I am unfamiliar with how the images are selected from the database and displayed, I am unable to code the image display itself. If this is using a WordPress custom field, it should have functions such as get_post_meta() in the code snippet, which should be quite clear to decipher and work through. 🙂

    I hope this helps. 🙂

    Oh wow! Thank you so much, I’ll try this all out and will let you know how it works. 🙂

    Awesome! Glad I could help. 🙂

    Did this solution work out?


    With leodata PHP is not problem.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Issue with custom PHP query’ is closed to new replies.