Custom FacetWP dropdown not showing posts - filtering

I have been able to render a dropdown facet in FacetWP front end with:
add_filter( 'facetwp_facet_html', function( $output, $params ) {
if ( 'today_or_month' == $params['facet']['name'] ) {
$selected = isset( $params['selected'] ) ? $params['selected'] : '';
$output = '<select class="facetwp-dropdown">';
$output .= '<option value="">Select date range</option>';
$output .= '<option value="today"' . ( 'today' == $selected ? ' selected' : '' ) . '>Today</option>';
$output .= '<option value="month"' . ( 'month' == $selected ? ' selected' : '' ) . '>This Month</option>';
$output .= '<option value="test"' . ( 'test' == $selected ? ' selected' : '' ) . '>Test</option>';
$output .= '</select>';
}
return $output;
}, 10, 2 );
All 'event' posts show under the Facet dropdown on page load but when I select an option from the dropdown the queries do not return any posts in the FacetWP template. When I select "Select date range" again all the posts show again.
The queries are as follows:
add_filter( 'facetwp_query_args', function( $args, $class ) {
if (!isset($class->http_params['get']['facetwp'])) return $args;
$selected = $class->http_params['get']['facetwp']['today_or_month'];
$current_month = date( 'Y-m' );
if ( 'today' == $selected ) {
$args['meta_query'][] = array(
'key' => 'event_date_start',
'value' => date( 'Y-m-d' ),
'compare' => '<=',
);
$args['meta_query'][] = array(
'key' => 'event_date_end',
'value' => date( 'Y-m-d' ),
'compare' => '>=',
);
}
elseif ( 'month' == $selected ) {
$args['meta_query'][] = array(
'key' => 'event_date_start',
'value' => date( 'Y-m-01' ),
'compare' => '>=',
);
$args['meta_query'][] = array(
'key' => 'event_date_end',
'value' => date( 'Y-m-t' ),
'compare' => '<=',
);
}
elseif ( 'test' == $selected ) {
$args['meta_query'][] = array(
'key' => 'event_date_start',
'value' => $current_month,
'compare' => 'LIKE',
);
}
$args['post_type'] = 'event';
return $args;
}, 10, 2 );
Does anybody know why the query is not returning any posts in the front end?
I thought maybe I need to convert to a timestamp from ACF custom fields first? Something like this:
$start_date = get_field( 'event_date_start', $params['post_id'] );
$end_date = get_field( 'event_date_end', $params['post_id'] );
$start_timestamp = strtotime( $start_date );
$end_timestamp = strtotime( $end_date );
And then use these variables as the key:
'key' => '$start_timestamp ',
Any help at all would be great. Been going over and over this! Thank you so much. I am begging.
I have tried to run the code as above, I expected some of the custom post types of event to show up in the FacetWP template area according to the dropdown selections. Instead no posts show at all (they should show as some of the posts have February dates).

Related

this plugin works only on page

I have a large amount of password-protected posts, all of which are hidden from the average viewer. I’d like a single password entry page to unlock the visibility of all the protected content, as well as a custom landing page after the successful password entry. This plugin seems to do what i’m looking for but only with Pages
can it also work on post and woocomerce products?
i added 'post' in 119 line, but not works
thanks
<?php
/*
define( 'SECONDS_TO_STORE_PW', 864000); // 864000 = 10 Days
class smartPWPages {
function smartpwpages_shortcode( $atts ) {
global $post;
extract( shortcode_atts( array(
'label' => __( 'Enter', 'smartpwpages' ),
'ID' => 'smartPWLogin',
'parent' => $post->ID,
), $atts ) );
$result = '<form ID="' . esc_attr( $ID ) . '" method="post" action="' . esc_url( get_permalink() ) . '" >' . PHP_EOL;
if ( isset( $_GET['wrongpw'] ) ) $result .= '<p id="smartPWError">' . __( 'You\'ve entered an invalid password.</p>', 'smartpwpages' ) . PHP_EOL;
$result .= ' <input class="requiredField" type="password" name="smartPassword" id="smartPassword" value=""/>' . PHP_EOL;
$result .= ' <input type="hidden" name="smartParent" value="' . (int) $parent . '" />' . PHP_EOL;
$result .= ' <input type="hidden" name="smartPWPage_nonce" value="' . wp_create_nonce( 'smartPWPage' ).'" />' . PHP_EOL;
$result .= ' <input type="submit" value="' . esc_attr( $label ). '" />' . PHP_EOL;
$result .= '</form>' . PHP_EOL;
return $result;
}
/**
* Password Redirect
* Decodes the password, stores it in a cookie and redirects the visitor to that page.
*/
function pw_redirect( $perma, $password ) {
global $wp_version, $wp_hasher;
// Version 3.6 introduces a new function
if ( function_exists( 'wp_unslash' ) ) {
$cookiePW = wp_unslash( $password );
} else {
$cookiePW = stripslashes( $password );
}
// Version 3.4 and higher has better security on the pw pages
if ( version_compare( $wp_version, '3.4', '>=' ) ) {
if ( empty( $wp_hasher ) ) {
// By default, use the portable hash from phpass
require_once( ABSPATH . 'wp-includes/class-phpass.php');
$wp_hasher = new PasswordHash( 8, true );
}
// Potentially using a custom hasher, hash the pw
$cookiePW = $wp_hasher->HashPassword( $cookiePW );
}
$secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
// Store password for the length in the constant
setcookie( 'wp-postpass_' . COOKIEHASH, $cookiePW, time() + SECONDS_TO_STORE_PW, COOKIEPATH, COOKIE_DOMAIN, $secure );
wp_safe_redirect( $perma );
exit();
}
/**
* Process Form
* Decodes the password submitted on a form, find a page that uses it and redirects the visitor to that page.
*/
function process_form() {
global $wp_version, $wp_hasher;
if ( isset( $_POST[ 'smartPassword' ] ) && isset( $_POST[ 'smartParent' ] ) && wp_verify_nonce( $_POST[ 'smartPWPage_nonce' ], 'smartPWPage' ) ) {
$parentForm = (int) $_POST[ 'smartParent' ] ;
$password = $_POST[ 'smartPassword' ];
if ( function_exists( 'wp_unslash' ) ) {
$postPassword = wp_unslash( $password );
} else {
$postPassword = stripslashes( $password );
}
$args = array(
'sort_order' => 'DESC',
'sort_column' => 'post_date',
'hierarchical' => 1,
'post_type' => 'page','post',
'post_status' => 'publish'
);
if ( function_exists( 'pause_exclude_pages' ) ) pause_exclude_pages();
$myPages = get_pages( $args );
if ( function_exists( 'resume_exclude_pages' ) ) resume_exclude_pages();
// Version 3.4 and higher has better security on the pw pages
if ( version_compare( $wp_version, '3.4', '>=' ) ) {
if ( empty( $wp_hasher ) ) {
// By default, use the portable hash from phpass
require_once( ABSPATH . 'wp-includes/class-phpass.php' );
$wp_hasher = new PasswordHash( 8, true );
}
}
foreach( $myPages as $page ) {
if ( ( $page->post_password == $postPassword ) || ( !empty( $wp_hasher ) &&
$wp_hasher->CheckPassword( $page->post_password, $postPassword ) ) ) {
$permalink = get_permalink( $page->ID );
$this->pw_redirect( $permalink, $postPassword );
}
}
// Nothing more to do here. If we reached here, we've submitted a pw but no match was found.
// Allow the page to continue loading, but hack $_GET to indicate the status
$_GET[ 'wrongpw' ] = TRUE;
}
}
}
) );
thanks to everyone can help me

Paragraph breaks missing from shortcode output

I created a shortcode in Wordpress to perform a query and display the content, but the content line breaks are being removed.
add_shortcode( 'resource' , 'Resource' );
function Resource($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'resource',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'operator' => 'AND',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
$string .= '<p>no listings at this time...</p>';
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="links"><div id="linksImage">' . get_the_post_thumbnail() . '</div>
<div id="linksDetails"><h1>'. get_the_title() .'</h1><p>' . get_the_content() . '</p>
<p>for more information CLICK HERE</div></div>';
}
wp_reset_postdata();
$output = '<div id="linksWrapper">' . $string . '</div>';
return $output;
}
Any suggestion on why this is happening and what to do to fix it. This is only happening on the shortcode output. On regular pages - the content displays correctly.
found a solution through more searches:
function get_the_content_with_formatting ($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
works perfect, so I thought I would share..

tax_query to get posts with a field with empty value?

I defined a custom field, to be able to hide posts from home, like this:
/* Campo adicional que puede ocultar noticia de la home */
function mnd_get_custom_field( $value ) {
global $post;
$custom_field = get_post_meta( $post->ID, $value, true );
if ( !empty( $custom_field ) )
return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
return false;
}
function mnd_add_custom_meta_box() {
add_meta_box( 'home-page', __( 'Home page', 'mnd' ), 'mnd_meta_box_output', 'post', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'mnd_add_custom_meta_box' );
function mnd_meta_box_output( $post ) {
wp_nonce_field( 'my_mnd_meta_box_nonce', 'mnd_meta_box_nonce' ); ?>
<p>
<label for="mnd_hide_homepage"><?php _e( 'Hide from homepage', 'mnd' ); ?>:</label>
<input type="checkbox" name="mnd_hide_homepage" id="mnd_hide_homepage" checked="<?php echo mnd_get_custom_field( 'mnd_hide_homepage' ) == 1 ? 'checked' : ''; ?>" value="1" size="50" />
</p>
<?php
}
function mnd_meta_box_save( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['mnd_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['mnd_meta_box_nonce'], 'my_mnd_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post', get_the_id() ) ) return;
if( isset( $_POST['mnd_hide_homepage'] ) )
update_post_meta( $post_id, 'mnd_hide_homepage', esc_attr( $_POST['mnd_hide_homepage'] ) );
}
add_action( 'save_post', 'mnd_meta_box_save' );
and I'm trying like:
// The Query
$ppp = 12;
$page = $_GET['page'];
$args_count = array(
'post_type' => 'post',
'paged' => false,
'status' => 'publish'
);
$posts_home_count = new WP_Query( $args_count );
$manyPosts = $posts_home_count->found_posts;
$paginas = $manyPosts / $ppp;
wp_reset_postdata();
$meta_params = array(
array(
'key' => 'mnd_hide_homepage',
'value' => 1,
'compare' => '!=',
'type' => 'NUMERIC'
)
);
$args = array(
'post_type' => 'post',
'posts_per_page' => $ppp,
'paged' => $page,
'page' => $page,
'status' => 'publish'/*,
'meta_query' => $meta_params */
);
//print_r ( $args );
$posts_home = new WP_Query( $args );
And it won't return any posts,
But if I change to
'compare' => '=',
Then it returns all the checked posts
Any idea what I'm missing?
There are some problems with Meta Query sometimes and the problem is not always visible. Try the following:
var_dump the WP_Query object and check the SQL manually. Do you see the problem?
Did you try to remove 'type' => 'NUMERIC'?
The “hack” solution (untested):
global $wpdb;
$ids = $wpdb->get_results("
SELECT $wpdb->posts.id FROM $wpdb->posts
WHERE post_status=publish
INNER JOIN $wpdb->post_meta
ON $wpdb->post_meta.post_id = $wpdb->posts.id
AND $wpdb->post_meta.key = 'mnd_hide_homepage'
AND $wpdb->post_meta.value != '1'
");
// put the ids with post__in into the WP_Query arguments
Maybe this is the solution. If not, it may help to find it.
Try this:
array(
'key' => 'mnd_hide_homepage',
'value' => '1',
'compare' => '!=',
'type' => 'NUMERIC'
)
Or
array(
'key' => 'mnd_hide_homepage',
'value' => array('1'),
'compare' => 'NOT IN'
)

current user entry counter gravity forms display numbers

how can i display numbers with shortcode of submitted from gravity form only from current logged in user
add_filter( 'gform_shortcode_entry_count', 'gwiz_entry_count_shortcode', 10, 2 );
function gwiz_entry_count_shortcode( $output, $atts ) {
extract( shortcode_atts( array(
'id' => false,
'status' => 'total', // accepts 'total', 'unread', 'starred', 'trash', 'spam'
'format' => false // should be 'comma', 'decimal'
), $atts ) );
$valid_statuses = array( 'total', 'unread', 'starred', 'trash', 'spam' );
if( ! $id || ! in_array( $status, $valid_statuses ) ) {
return current_user_can( 'update_core' ) ? __( 'Invalid "id" (the form ID) or "status" (i.e. "total", "trash", etc.) parameter passed.' ) : '';
}
$counts = GFFormsModel::get_form_counts( $id );
$output = rgar( $counts, $status );
if( $format ) {
$format = $format == 'decimal' ? '.' : ',';
$output = number_format( $output, 0, false, $format );
}
return $output;
}

How to make an activity to everyone to see on Social Engine?

I am trying to make an activity published by an admin to be visible to every member of the network, not just their friends.
As far as I could make until now, is that the table stream is preventing my message to became public, it only make a profile message. Visible only in its feed.
Does anyone have any idea how to enforce this?
[EDIT]
My code below is picking tweets in my account, I am using some settings i created (like the screen_name from twitter and the user who is publishing the tweet at the feed).
The code is doing as follows:
-> Checking if there is settings for twitter user to read its messages.
-> Save it in a table
-> Authorizing (in theory) to everyone to read this messages, ether is friend of the publisher or not.
-> Adding the activity, that I also created.
$TwitterUser = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'core.twitter.screen_name' );
if ($TwitterUser != '') {
// Ultimo tweet postado
$TwitterUser = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'core.twitter.screen_name' );
$table = Engine_Api::_ ()->getDbtable ( 'tweets', 'user' );
$query = $table->select ()->order ( 'tweets_id desc')->limit(1);
$lastTweet = $table->fetchRow ( $query );
// Set the configuration parameters
$config = array ('adapter' => 'Zend_Http_Client_Adapter_Socket', 'ssltransport' => 'tls' );
// Instantiate a client object
if (is_null ( $lastTweet )) {
$client = new Zend_Http_Client ( "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$TwitterUser&count=4&include_rts=1", $config );
} else {
$client = new Zend_Http_Client ( "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$TwitterUser&include_rts=1&since_id=" . $lastTweet->real_id, $config );
}
// The following request will be sent over a TLS secure connection.
$response = $client->request ();
// Pegando a resposta
$body = json_decode ( $response->getBody () );
if (! empty ( $body )) {
$db = Engine_Api::_()->getDbtable('tweets', 'user')->getAdapter();
$db->beginTransaction();
try
{
$table = Engine_Api::_ ()->getDbtable ( 'users', 'user' );
$twitter_user = $table->fetchRow ( $table->select ()->where ( 'user_id = ?', Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'core.twitter.user_id' ) ) );
$table = Engine_Api::_ ()->getDbtable ( 'tweets', 'user' );
$activityApi = Engine_Api::_ ()->getDbtable ( 'actions', 'activity' );
foreach ( array_reverse($body) as $tweet ) {
$data = explode ( ' ', $tweet->created_at );
$meses = array ('Jan' => '01', 'Fev' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12' );
$tweet_date = $data ['5'] . "-" . $meses [$data [1]] . '-' . $data [2] . " " . $data [3];
$tweet_date = (date ( 'Y-m-d H:i:s', strtotime ( $tweet_date ) ));
$last_tweet = $table->createRow();
$last_tweet->setFromArray(array(
'real_id' => $tweet->id_str,
'user_id' => $tweet->user->id_str,
'screen_name' => $tweet->user->screen_name,
'body' => $tweet->text,
'retweeted' => $tweet->retweeted,
'created_at' => $tweet_date,
'profile_image_url' => $tweet->user->profile_image_url,
'coordinates' => $tweet->coordinates
));
$last_tweet->save();
// Authorizations
$auth = Engine_Api::_()->authorization()->context;
$auth->setAllowed($last_tweet, 'everyone', 'view', true);
$auth->setAllowed($last_tweet, 'everyone', 'comment', true);
$auth->setAllowed($last_tweet, 'everyone', 'likes', true);
$action = $activityApi->addActivity ($twitter_user,$last_tweet, 'post_twitter', $tweet->text);
if( $action ) {
$activityApi->attachActivity($action, $last_tweet);
}
}
// Commit
$db->commit();
} catch ( Exception $e ) {
$db->rollBack ();
throw $e;
}
}
}
No problems happens, but the tweet that I am tracking only could be red by the user I used to publish it.
You should create entries in stream table for this action_id.