• Hi,

    I have a bit of code that queries a particular category in WordPress and returns the last 4 results. This works fine unless it is called from a page where it is integrated within Magento or WHMCS. In those cases, instead of pulling the results from the specified category, it seems to be querying the last post, and displaying it 4 times.

    For example, here is the include file showing the wanted output:

    https://www.thinkshovels.com/includes/latest_work.php but if you look here on a page where whmcs is in use, you see it pulls the latest post from wordpress instead, https://www.thinkshovels.com/service

    Here is the code generating the posts to be displayed:

    define(‘WP_USE_THEMES’, false);
    require(‘wp-load.php’);

    $qarray = array(‘cat’ => ‘5’, ‘posts_per_page’ => 4);
    query_posts($qarray);

    while (have_posts()): the_post();

    $args = array(
    ‘post_type’ => ‘attachment’,
    ‘numberposts’ => -1,
    ‘post_status’ => null,
    ‘post_parent’ => $post->ID
    );`

    Any help on what I need to do to get this to pull the correct content is much appreciated!

    Thanks in advance.

Viewing 1 replies (of 1 total)
  • Thread Starter thinkshovels

    (@thinkshovels)

    Turns out using query_posts was the problem. Updated Code that is working fine:

    The synopsis of this code is to read the last 4 posts from category 5, then grab the first attachment associated with that post.

    define('WP_USE_THEMES', false);
    	require('wp-load.php');
    	$qarray = array('category' => '5', 'posts_per_page' => 4);
            $posts = get_posts($qarray);
    
    	foreach($posts as $post){
    		$args = array( 'post_type' => 'attachment',                           'numberposts' => 1, 'post_status' =>'any', 'post_parent' => $post->ID );
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ( $attachments as $attachment ) {
                    // Do Stuff here
             }
    }
    }
Viewing 1 replies (of 1 total)
  • The topic ‘WordPress category query from Magento/WHMCS returns wrong result’ is closed to new replies.