WordPress.org

Ready to get started?Download WordPress

Forums

Events Manager
[resolved] Displaying events with WP_QUERY, tags not working (9 posts)

  1. Paskadox
    Member
    Posted 11 months ago #

    I'm creating a custom widget that will display a list of featured events in a slideshow on the homepage. I've got a query going that gets the last few events, but I only want to display events that are tagged as featured. Putting tag => featured in the array returns no results, even though I'm positive that one of the events is tagged as featured. My guess is that the events tags and WordPress post tags are different things, and I'm using the wrong call. How do I query only those events tagged "Featured"?

    My code is here:

    function my_em_wp_query(){
    	$args = array(
    		'post_type' => 'event',
    		'posts_per_page' => 5,
    		'meta_query' => array( 'key' => '_start_ts', 'value' => current_time('timestamp'), 'compare' => '>=', 'type'=>'numeric' ),
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		'meta_key' => '_start_ts',
    		'meta_value' => current_time('timestamp'),
    		'meta_value_num' => current_time('timestamp'),
    		'meta_compare' => '>=',
    		'tag' => 'featured'
    	);
    
    	// The Query
    	$query = new WP_Query( $args );
    
    	// The Loop
    	while($query->have_posts()):
    	$query->next_post();
    	$id = $query->post->ID;
    	echo '<li>';
    	echo '<a href="' . get_permalink($id) . '">' . get_the_title($id) . '</a>';
    	echo ' - '. get_post_meta($id, '_event_start_date', true);
    	echo the_field('format', $id);
    	echo the_field('entry_fee', $id);
    	echo ' - '. get_post_meta($id, 'event-tag', true);
    	echo '</li>';
    	endwhile;
    
    	// Reset Post Data
    	wp_reset_postdata();
    }
    
    echo my_em_wp_query();

    http://wordpress.org/extend/plugins/events-manager/

  2. angelo
    NetWebLogic Support
    Posted 11 months ago #

    you need to include taxonomy parameters for this to work - https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

  3. Paskadox
    Member
    Posted 11 months ago #

    I'm pretty confused about what this means, and nothing I try works. I want to display all posts tagged "featured". Surely there must be a simple way to do this? Why doesn't just adding "tag => 'featured'" work?

    $args = array(
    		'post_type' => 'event',
    		'posts_per_page' => 5,
    		'meta_query' => array( 'key' => '_start_ts', 'value' => current_time('timestamp'), 'compare' => '>=', 'type'=>'numeric' ),
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		'meta_key' => '_start_ts',
    		'meta_value' => current_time('timestamp'),
    		'meta_value_num' => current_time('timestamp'),
    		'meta_compare' => '>=',
    		'tax_query' => array(
    		array(
    			'taxonomy' => 'post_tag',
    			'field' => 'slug',
    			'terms' => array( 'featured' )
    		)
    	)
  4. caimin_nwl
    NetWebLogic Support
    Posted 11 months ago #

    The code snippet is missing the final ) - is that missing in your original file?

  5. Paskadox
    Member
    Posted 11 months ago #

    Oh no, it's not missing, just cut off. If someone could give me an example WP_Query that queries all events with a specific event tag, I'd appreciate it!

  6. Paskadox
    Member
    Posted 11 months ago #

    I'm thinking an additional meta_query would do it, but I don't know how to write it.

  7. angelo
    NetWebLogic Support
    Posted 11 months ago #

  8. hristo5322
    Member
    Posted 11 months ago #

    I did something like this with a little change in DB in wp_em_events I added column named 'recomended' and then changed this

    /*wp-content/plugins/events-manager/templates/forms/event/attributes.php*/
    
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <?php
    global $EM_Event;
    $attributes = em_get_attributes();
    $has_depreciated = false;
    ?>
    <div id="event-attributes">
    <?php
    if($_GET['action'] != 'edit'){
        //do nothing
    } else {
        $q = 'SELECT recomended FROM wp_em_events WHERE post_id =' . $_GET['post'] . ' LIMIT 1';
        $r = mysql_query($q);
        $attr_result = mysql_fetch_assoc($r);
        if($attr_result['recomended'] == 0){
            $rec_img = '<img src="http://ideas4web.eu/sedmicata.com/wp-content/themes/sedmicata/images/off.png" style="cursor: pointer;" class="recomend" title="Включи">';
        } else {
            $rec_img = '<img src="http://domain/wp-content/themes/sedmicata/images/on.png" style="cursor: pointer;" class="unrecomend" title="Изключи">';
        }
    ?>
            <table class="form-table">
                <tr>
                    <td>
                        <span style="font-size: 20px; line-height: 27px; margin-right: 10px;">Recomended</span><?php echo $rec_img; ?>
                    </td>
                </tr>
            </table>
    <?php
    }
    ?>
    	<?php if( !empty($attributes['names']) && count( $attributes['names'] ) > 0 ) : ?>
    		<table class="form-table">
    			<thead>
    				<tr valign="top">
    					<td><strong>Attribute Name</strong></td>
    					<td><strong>Value</strong></td>
    				</tr>
    			</thead>
    			<tbody id="mtm_body">
    				<?php
    				$count = 1;
    				foreach( $attributes['names'] as $name){
    					?>
    					<tr valign="top" id="em_attribute_<?php echo $count ?>">
    						<td scope="row"><?php echo $name ?></td>
    						<td>
    							<?php if( count($attributes['values'][$name]) > 1 ): ?>
    							<select name="em_attributes[<?php echo $name ?>]">
    								<?php foreach($attributes['values'][$name] as $attribute_val): ?>
    									<?php if( array_key_exists($name, $EM_Event->event_attributes) && $EM_Event->event_attributes[$name]==$attribute_val ): ?>
    										<option selected="selected"><?php echo $attribute_val; ?></option>
    									<?php else: ?>
    										<option><?php echo $attribute_val; ?></option>
    									<?php endif; ?>
    								<?php endforeach; ?>
    							</select>
    							<?php else: ?>
    							<input type="text" name="em_attributes[<?php echo $name ?>]" value="<?php echo array_key_exists($name, $EM_Event->event_attributes) ? esc_attr($EM_Event->event_attributes[$name], ENT_QUOTES):''; ?>" />
    							<?php endif; ?>
    						</td>
    					</tr>
    					<?php
    					$count++;
    				}
    				if($count == 1){
    					?>
    					<tr><td colspan="2"><?php echo sprintf(__("You don't have any custom attributes defined in any of your Events Manager template settings. Please add them the <a href='%s'>settings page</a>",'dbem'),EM_ADMIN_URL ."&page=events-manager-options"); ?></td></tr>
    					<?php
    				}
    				?>
    			</tbody>
    		</table>
    		<?php if( count(array_diff(array_keys($EM_Event->event_attributes), $attributes['names'])) > 0 ): ?>
    		<p><strong><?php _e('Depreciated Attributes', 'dbem')?></strong></p>
    		<p><em><?php _e("If you see any attributes under here, that means they're not used in Events Manager formats. To add them, you need to add the custom attribute again to a formatting option in the settings page. To remove any of these depreciated attributes, give it a blank value and save.") ?></em></p>
    		<table class="form-table">
    			<thead>
    				<tr valign="top">
    					<td><strong>Attribute Name</strong></td>
    					<td><strong>Value</strong></td>
    				</tr>
    			</thead>
    			<tbody id="mtm_body">
    				<?php
    				if( is_array($EM_Event->event_attributes) and count($EM_Event->event_attributes) > 0){
    					foreach( $EM_Event->event_attributes as $name => $value){
    						if( is_array($value) ) $value = serialize($value);
    						if( !in_array($name, $attributes['names']) ){
    							?>
    							<tr valign="top" id="em_attribute_<?php echo $count ?>">
    								<td scope="row"><?php echo $name ?></td>
    								<td>
    									<input type="text" name="em_attributes[<?php echo $name ?>]" value="<?php echo esc_attr($value, ENT_QUOTES); ?>" />
    								</td>
    							</tr>
    							<?php
    							$count++;
    						}
    					}
    				}
    				?>
    			</tbody>
    		</table>
    		<?php endif; ?>
    	<?php else : ?>
    		<p>
    		<?php _e('In order to use attributes, you must define some in your templates, otherwise they\'ll never show. Go to Events > Settings > General to add attribute placeholders.', 'dbem'); ?>
    		</p>
    		<script>
    			jQuery(document).ready(function($){ $('#event_attributes').addClass('closed'); });
    		</script>
    	<?php endif; ?>
    </div>
    <script>
    window.onload = function() {
    /* attach a submit handler to the form */
    jQuery(document).on("click", ".unrecomend", function(){
        /* Remove current class of all other elements */
        $(this).removeClass('recomended');
         /*Add current class to the selected element */
        $(this).addClass('recomend');
        $(this).attr('src', 'http://ideas4web.eu/sedmicata.com/wp-content/themes/sedmicata/images/off.png');
        $(this).attr('title', 'Включи');
      /* Send the data using post */
        $.post("http://ideas4web.eu/sedmicata.com/wp-content/plugins/events-manager/templates/forms/event/commit.php?attr=rec", { setStr: "0", pid: "<?php echo $_GET['post']; ?>" });
    });
    
    jQuery(document).on("click", ".recomend", function(){
        /* Remove current class of all other elements */
        $(this).removeClass('recomend');
        /* Add current class to the selected element */
        $(this).addClass('unrecomend');
        $(this).attr('src', 'http://domain/wp-content/themes/sedmicata/images/on.png');
        $(this).attr('title', 'Изключи');
      /* Send the data using post */
        $.post("http://domain/wp-content/plugins/events-manager/templates/forms/event/commit.php?attr=rec", { setStr: "1", pid: "<?php echo $_GET['post']; ?>" });
    });
    
    };
    </script>
    
    /*wp-content/plugins/events-manager/templates/forms/event/commit.php*/
    $dbuser = 'dbuse'; //Enter BD user
    $dbpass = 'dbpass'; //Enter DB pass
    $dbhost = 'localhost'; //Enter DB host if differs from localhost
    $dbconn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(!dbconn){
        die('Could not connect: ' . mysql_error());
        }
    
        $dbselect = mysql_select_db('database');
    
    $attr = $_GET['attr'];
    switch ($attr){
        case 'rec':
                $q = "UPDATE wp_em_events SET recomended = " . $_POST['setStr'] . " WHERE post_id = " . $_POST['pid'] . ' LIMIT 1';
                $r = mysql_query($q);
        break;
    
        default:
            // Do nothing
        break;
    }
    
    /*http://ideas4web.eu/sedmicata.com/wp-content/plugins/events-manager/widgets/em-events.php*/
    
    		$this->em_orderby_options = apply_filters('em_settings_events_default_orderby_ddm', array(
    			'event_start_date,event_start_time,event_name' => __('start date, start time, event name','dbem'),
    			'event_name,event_start_date,event_start_time' => __('name, start date, start time','dbem'),
    			'event_name,event_end_date,event_end_time' => __('name, end date, end time','dbem'),
    			'event_end_date,event_end_time,event_name' => __('end date, end time, event name','dbem'),
                'recomended,event_id' => __('recomended, event id','dbem') /*this creates custom order by*/

    [Please post code or markup between backticks or use the code button. Or better still - use a pastebin. Your posted code has now been permanently damaged/corrupted by the forum's parser.]

    and then I used the widget to display the events i need

  9. bibichette
    Member
    Posted 11 months ago #

    Hi,

    I think that you can use our plugin named Inject for your need. You can make complex queries easily. Take a look : http://cut.lu/inject and the doc for custom query : http://inject.netcod.es/documentation/#custom-fields :)

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.