• I am trying to run an extra query within the archive.php. But everytime time it executes, it returns the same results which are not the results that I am after.

    Below is a copy of my code that I am running in the archive file, which is located before the if (have_posts()) : while (have_posts()) : the_post(); code.

    The code should get the slug of the tag that I am viewing, and use that to check if there is another post in a different post type with the same slug.

    $tag       = get_queried_object();
    $tag_title = $tag->name; // Same as single_tag_title()
    $tag_slug  = $tag->slug;
    $tag_id    = $tag->term_id;
    $CheckForPost = array(
    	'post_type' => 'player-profiles',
    	'post_status' => 'publish',
    	'posts_per_page' => 1,
    	'post_slug' => '$tag_slug'
    );
    $GetProfileCheck = null;
    $GetProfileCheck = new WP_Query($CheckForPost);
    if (have_posts()){
    	// return true
    }else{
    	// return false
    }
Viewing 1 replies (of 1 total)
  • have_posts() is a template tag, meaning, it will use the current loop/query. WP_Query is an object (class) to create a secondary loop. Your example:

    $GetProfileCheck = null; // fyi this is unnecessary
    $GetProfileCheck = new WP_Query($CheckForPost);
    if ($GetProfileCheck->have_posts()){
    	// return true
    }else{
    	// return false
    }

    I commend you for not copping out and using query_posts() btw.

Viewing 1 replies (of 1 total)
  • The topic ‘Query posts in archive.php’ is closed to new replies.