Forum Replies Created

Viewing 11 replies - 1 through 11 (of 11 total)
  • Forum: Hacks
    In reply to: WP_Query tax_query in plugin
    Thread Starter cboakes

    (@cboakes)

    Thanks for taking the time to reply bcworkz.

    I’m trying to write a plugin that returns JSON encoded results. A bit like JSON API but with the ability to query ACF fields and multiple taxonomy terms.

    I’ve constructed my query (basic example in the question) which, if I leave out the tax_query part works fine. However with the tax_query as an argument it doesn’t return anything.

    I wasn’t sure if WP_Query was the best way to approach this kind of query or whether I need to go down the $wpdb route.

    Thread Starter cboakes

    (@cboakes)

    For anyone who is interested:

    http://wordpress.org/support/topic/328118

    Thread Starter cboakes

    (@cboakes)

    Hi,

    Thanks for the reply but I’ve just figured it out. Here’s how I ended up doing it. I realise that this may not the most perfect method but it works. If you have any suggestions post them up.

    To clarify: the following code looks to see if a post has more than one tag, if it does it displays that tag as a header and displays the other tags with that tag below. If the post only has one tag it displays it as a link.

    To demonstrate.
    Post 1 has tags of ‘Music’ and ‘Rock’
    Post 2 has tags of ‘Music’ and ‘Jazz’
    Post 3 has a tag of ‘My Band’

    It would look like this:
    -Music
    –Rock
    –Jazz
    -My Band

    <?php
    	//Set up an array to put all of the tags that have been used in drop-dwon
    	$used = array();
    	//Get all the tags
    	$tags = get_tags(array('orderby' => '', 'order' => 'ASC') );
    	//For each tag
    	foreach ($tags as $tag) {
    		$post_tag = get_term($tag, 'post_tag');
    		//Get posts limited only to the current tag
    		$t_posts=get_posts(array('orderby' => 'date', 'tag__in' => array($tag->term_id)));
    		//If the post has more than one tag
    		if ($post_tag->count > 1) {
    			//Display the drop down title (e.g if two posts were tagged with 'Music' this would diaply that 'Music' title
    			echo '<h4 id="' . $tag->term_id .'">' . $tag->name . '</h4> <br />';
    			//Declaring the title as a variable for later use
    			$taghead = $tag->name;
    			//For each post with the current tag
    			foreach($t_posts as $t_post) {
    				setup_postdata($t_post);
    				//Get all of the posts under the current title
    				$the_tags = get_the_tags($t_post->ID); foreach($the_tags as $the_tag) {
    				//Don't diaply the current tags title
    				if ($the_tag->name == $taghead) continue;
    				//Display the tag under the title
    				echo '<p class="tag-' . $the_tag->term_id . '"><a href="' . get_tag_link( $the_tag->term_id ) . '">' . $the_tag->name . '</a></p>';
    				//Push the current tag into the used array
    				array_push($used, $the_tag->term_id);
    			} //End foreach tag
    		} //End foreach
    		//If the post only has one tag then link to it as an archive
    		} else {
    			//If the tag has already been displayed above don't show it here
    			if (in_array($tag->term_id, $used)) continue;
    			//Display the rest of the tags
    			echo '<h4 class="tag-' . $tag->term_id . '"><a href="'. get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a></h4><br />';
    		} // End else
    ?>
    Thread Starter cboakes

    (@cboakes)

    I’ve tried my best to do this but it isn’t working for me – I know it’s a lot to ask but is there a way you could give me an example please?

    Thread Starter cboakes

    (@cboakes)

    Does anyone know how I might even start to do this – I’m really stuck?

    Thanks

    Thread Starter cboakes

    (@cboakes)

    I figured it out, here’s how I did it for anybody interested:

    <?php
    	$tags = get_tags( array('orderby' => 'name', 'order' => 'ASC') );
    	foreach ( (array) $tags as $tag ) {
    		$args=array('orderby' => 'date', 'showposts' => -1, 'tag__in' => array($tag->term_id), 'caller_get_posts'=>1);
    		$posts=get_posts($args);
    		echo '<h2>' . $tag->name . '</h2> <br />';
    		foreach($posts as $post) {
    			setup_postdata($post); ?>
    			<p><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p>
    <?php }	} ?>

    I found this topic on how to do the same thing with categories extremely useful 🙂

    Thanks for your help!

    Thread Starter cboakes

    (@cboakes)

    Thanks for the response Michael.

    I think I see what you mean, so, for instance if I was going to display the posts ID under the tags title I would do as follows:

    <?php $posthead = get_posts(array('orderby' => 'name', 'order' => 'ASC', 'category' => '3,5,6,7,8'));	foreach($posthead as $post) : setup_postdata($post); ?>
     		<?php $posttags = get_the_tags($post->ID);
    		foreach($posttags as $tag) {
    			echo '<h2>' . $tag->name . '</h2>';
    			echo '<p>' . $post->ID . '</p>';
    		}
    	?>
     <?php endforeach; ?>

    However my main problem is that it will display the tag title and then the post title underneath for each post. So for example if I had two posts tagged with ‘music’, then it would look like this:

    Music
    band_1
    Music
    band_2

    Thanks

    Thread Starter cboakes

    (@cboakes)

    Figured it out, thanks to this article:

    http://www.mydigitallife.info/2006/06/24/retrieve-and-get-wordpress-post-id-outside-the-loop-as-php-variable/

    So the variable needed to be declared as:
    $post->ID

    Thread Starter cboakes

    (@cboakes)

    I’ve figured out what I think is the best way to approach it but I still need help with its completion:
    I use:
    <?php query_posts(array('category_name' => 'news', 'post__not_in' => array(31))); ?>
    This would exclude, in this instance, the post with the ID of 31.

    So now I need to make it dynamic. My knowledge of php isn’t great, but I thought declaring a variable of the current post would work, so something like:

    <?php
    $excludeid = 31;
    query_posts(array('category_name' => 'news', 'post__not_in' => array($excludeid)));
    ?>

    This worked fine. So to make it dynamic my logic was to go with:
    $excludeid = the_ID();

    But this just doesn’t work, would anybody help me to try and make this dynamic?

    Thanks

    Thread Starter cboakes

    (@cboakes)

    I did notice that – it is an issue that I’m going to try and solve.

    What do you think would be the best way to approach it?

    Thanks

    Thread Starter cboakes

    (@cboakes)

    Thanks esmi,

    I managed to figure out by simply looking up the ‘wp_list_pages’ in the codex, there I was able to find that there is in fact a parameter that can be passed called ‘link_after’, which does what I want. So to display a / character after each link I would simply just pass the parameter like this:

    wp_list_pages(‘title_li=0&sort_column=menu_order&link_after= /’);

    Chris

Viewing 11 replies - 1 through 11 (of 11 total)