WordPress.org

Ready to get started?Download WordPress

Forums

Custom query stopped working (2 posts)

  1. brettweaver
    Member
    Posted 1 year ago #

    A couple of years ago I hacked the following query together and it worked fine until recently. I should know, but I don't know exactly when it broke. I suspect there's some deprecated statement in there somewhere with one of the newer versions of WP, but I'm not savvy enough to spot it.

    This is a site used to display vehicles for sale. Each vehicle gets a WP Page that is set with a parent based on year-model grouping. I'm using custom fields for the vehicle meta data. This query looks at the current page then is supposed to pull a list of the vehicles (Pages) that: 1. Are Published, 2. Have a status of ForSale, 3. Have an item type of "Vehicle" and 4. are a child of the current page. Here's the code:

    <!--Custom query to select only pages that are Vehicles for sale and children of the current page -->
    <?php
    	$querystr = "
    		SELECT * FROM $wpdb->posts
    
    		LEFT JOIN $wpdb->postmeta AS status ON(
    		$wpdb->posts.ID = status.post_id
    		AND status.meta_key = 'Status'
    		)
    		LEFT JOIN $wpdb->postmeta AS itemtype ON(
    		$wpdb->posts.ID = itemtype.post_id
    		AND itemtype.meta_key = 'ItemType'
    		)
    		WHERE $wpdb->posts.post_status = 'publish'
    		AND $wpdb->posts.post_type='page'
    		AND $wpdb->posts.post_parent='.$post->ID.'
    		AND status.meta_value='ForSale'
    		AND itemtype.meta_value='Vehicle'
    		ORDER BY $wpdb->posts.post_date DESC
    	";
    
    	$pageposts = $wpdb->get_results($querystr, OBJECT);
    
    ?>

    Interestingly the code below still works on a page where I'm pulling all vehicles for sale without regard for the page's parent:

    <!--Custom query to select only pages that are Vehicles For Sale -->
    <?php
    
    	$querystr = "
    		SELECT * FROM $wpdb->posts
    
    		LEFT JOIN $wpdb->postmeta AS status ON(
    		$wpdb->posts.ID = status.post_id
    		AND status.meta_key = 'Status'
    		)
    		LEFT JOIN $wpdb->postmeta AS itemtype ON(
    		$wpdb->posts.ID = itemtype.post_id
    		AND itemtype.meta_key = 'ItemType'
    		)
    		WHERE $wpdb->posts.post_status = 'publish'
    		AND $wpdb->posts.post_type='page'
    		AND status.meta_value='ForSale'
    		AND itemtype.meta_value='Vehicle'
    		ORDER BY $wpdb->posts.post_date DESC
    	";
    
    	$pageposts = $wpdb->get_results($querystr, OBJECT);
    
    ?>

    So I suspect it's that last join for the parent criteria, but as I said I've exhausted my PHP skills at this point. Any help would be appreciated.

    Thanks,
    Brett

  2. brettweaver
    Member
    Posted 1 year ago #

    I realized I may not have stated the problem clearly. The first code block simply returns no query results, even though there are pages in the database that meet the criteria. It was working, but stopped working suddenly.

Topic Closed

This topic has been closed to new replies.

About this Topic