• Hi there,

    For the life of me I can’t work out how to do this one. If anyone can help I will be very very appreciative! Thanks in advance.

    I have two arrays each containing category names as values so for instance

    Array1 ( [0] => cheap [1] => cheap [2] => average )
    Array2 ( [0] => hiphop [1] => charthits [2] => partybangers )

    These arrays change each time the page is reloaded to create a more random variation to display to the user

    The keys for each array match, so I need to get a list of all post id’s for any post tagged in both:
    0 > cheap & hiphop
    1 > cheap & charthits
    2 > average & partybangers

    But how would I retrieve an array containing the post id’s?? wp_query?

    Many thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Right, WP_Query. Specifically you would provide a ‘tax_query’ argument array. See Class_Reference/WP_Query#Taxonomy_Parameters for more information.

    You will not get an array of IDs this way, you actually get an array of post objects, within each object would be the ID of course. Exactly how you use WP_Query would depend on what and where your are displaying the results. If this query is to be used as the main page content, you should alter the query by hooking ‘pre_get_posts’ and setting the ‘tax_query’ query var.

    If the results are displayed in addition to the main page content, you can use get_posts() or create your own instance of a query object.

    Thread Starter bossbowser

    (@bossbowser)

    Thanks @bcworkz

    Adding onto my question. Currently I’m able to get it to spit out posts tagged in either a or b. But how do I change this to only get get those tagged in BOTH a & b.

    $args = array (
    ‘meta_key’ =>’ratings_average’,
    ‘orderby’=>’meta_value_num’,
    ‘order’ =>’DESC’,
    ‘posts_per_page’ =>-1,
    ‘post_type’ => ‘post’,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘Music’,
    ‘field’ => ‘slug’,
    ‘terms’ => array( ‘reggae-2’, ‘hiphop-2’ )
    ),
    ),);

    So as it stands anything with Reggae or House will be displayed, but I’m after the posts that are tagged in both music types not either.

    Anyone done this before? Google giving me nothing so far.

    Thread Starter bossbowser

    (@bossbowser)

    Ok so I’m almost there. From what I can understand the following code should work with the logic AND as default. But strangely it is only spitting out the first term named ‘mon’ rather than both of ‘mon’ + ‘house-3’.

    Any thoughts?

    $testing = 'mon';
       $testing2 = 'house-3';
    
    		$args = array (
    		'meta_key' =>'ratings_average',
    		'orderby'=>'rand',
    		'order' =>'DESC',
    		'posts_per_page' =>1,
    		'post_type' => 'post',
    		'tax_query' => array(
    		array(
    			'taxonomy' => 'Music',
    			'field' => 'slug',
    			'terms' => $testing)
    		),
    		array(
    			'taxonomy' => 'Music',
    			'field' => 'slug',
    			'terms' => $testing2)
    		);
    				$new_query = new WP_Query($args);
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get post ids by category name’ is closed to new replies.