Forum Replies Created

Viewing 15 replies - 1 through 15 (of 19 total)
  • Thread Starter niemand_0

    (@niemand_0)

    Oh, interesting!

    The Relevanssi-generated excerpts are there for all posts, in the post_excerpt property.

    It isn’t there, however, in my $post variable:

    foreach($search_results->posts as $key => $result) {
    	$post = get_post($result->ID);
    	include('inc--search-preview.php');
    }

    Is it because I’m not in the WordPress “loop”? I tried that but I get a “have_posts() is not defined” error:

    while($search_results->have_posts()) : $search_results->the_post();
    	include('inc--search-preview.php');
    endwhile;

    Am I missing something basic here? I haven’t built a query this way before so I’m not sure how to loop through it the “WordPress way”, rather than just doing a standard php foreach loop.

    Thread Starter niemand_0

    (@niemand_0)

    Great idea with running the query directly on the database.

    That turned out to not shed any light on the issue (other than that it returned the higher number as well), but using relevanssi_do_query in search.php (so I had the exact same code in both search.php and functions.php) and comparing the search results post by post did:

    Turns out the query in functions.php returns posts with status “draft” and “pending review” as well as “published”. Adding post_status = 'publish' fixed it:

    global $query;
    $search_results = $query;
    
    $search_results->query_vars['s'] = 'wow';
    
    $search_results->query_vars['posts_per_page'] = -1;
    $search_results->query_vars['post_status'] = 'publish';
    $search_results->query_vars['offset'] = 0;
    
    relevanssi_do_query($search_results);

    However, now I have two more issues/questions for you.

    In search.php (using the same code as above), relevanssi excerpts are only be generated for the first 10 posts. I am using get_the_excerpt().

    What is the best way to go about ordering posts with the same relevance_score? I would like posts with a more recent date to show up first.

    • This reply was modified 7 years, 4 months ago by niemand_0. Reason: cleaner code

    Do a var_dump($custom_fields) to make sure the strings in your $unwanted_fields array are actually the field names being used in $custom_fields.

    You’ll see the result of the var_dump() on the Relevanssi admin page when you reindex.

    • This reply was modified 7 years, 4 months ago by niemand_0.
    Thread Starter niemand_0

    (@niemand_0)

    Thank you! I knew it was probably me doing something funky.

    Much appreciated.

    Thread Starter niemand_0

    (@niemand_0)

    Will do, thank you!

    Hey @danieltj. Did you figure out a solution to this? I’m trying to figure out the exact same thing but can’t find anything short of checking the password cookie myself. It seems like a real oversight that post_password_required() doesn’t provide any sign of whether the user authenticated correctly.

    Thread Starter niemand_0

    (@niemand_0)

    Entschuldigung, Ich habe alle meine Deutsch vergessen…Dank für in Englisch zu schreiben ??

    It goes in a javascript file that you’re loading on the front end, within the document ready call. For example, I put the code in a file named site.js which I’m calling in the footer of my theme.

    Thread Starter niemand_0

    (@niemand_0)

    Perfect, thanks Charlie!

    Thread Starter niemand_0

    (@niemand_0)

    Here’s a solution for half stars in the meantime.

    Note: This uses Font Awesome, which must be installed for this to work.

    var ratingContainer = $('span[itemprop="aggregateRating"]'),
    	starsContainer = $('.stars', ratingContainer),
    	totalStars = Math.round(parseFloat($('.rating', ratingContainer).text()) * 2) / 2,
    	wholeStars = Math.floor(totalStars),
    	emptyStars = 5 - Math.ceil(totalStars);
    
    // empty the stars container element
    starsContainer.html('');
    
    // add whole stars
    for (var i = 1; i <= wholeStars; i++) {
    	starsContainer.append('<i class="fa fa-star" aria-hidden="true"></i>');
    }
    
    // add a half star if applicable
    if (totalStars - wholeStars !== 0) {
    	starsContainer.append('<i class="fa fa-star-half-o" aria-hidden="true"></i>');
    }
    
    // add remaining empty stars
    for (var i = 1; i <= emptyStars; i++) {
    	starsContainer.append('<i class="fa fa-star-o" aria-hidden="true"></i>');
    }
    
    // [OPTIONAL] switch the individual reviews' stars to font awesome for consistency
    var indivReviews = {
    	ratingContainer : null,
    	totalStars : null
    };
    
    $('.full-testimonial').each(function() {
    	indivReviews.ratingContainer = $(this).find('.stars').html('');
    	indivReviews.totalStars = parseInt($(this).find('div[itemprop="reviewRating"] > span[itemprop="ratingValue"]').text());
    
        for (var i = 1; i <= indivReviews.totalStars; i++) {
        	indivReviews.ratingContainer.append('<i class="fa fa-star" aria-hidden="true"></i>');
        }
    
        for (var i = 1; i <= (5 - indivReviews.totalStars); i++) {
        	indivReviews.ratingContainer.append('<i class="fa fa-star-o" aria-hidden="true"></i>');
        }
    
    });
    Thread Starter niemand_0

    (@niemand_0)

    Oh awesome, I didn’t realize I had access to the richReviews instance in my template files.

    Ok, so I have that code in place but for some reason it’s returning with a count of zero reviews (even though I have three approved reviews on that page).

    Here’s my code:

    global $richReviews;
    
    $reviewData = $richReviews->db->get_average_rating('any', $post);
    $reviewCount = $reviewData['reviewsCount'];
    
    print_r($reviewData);

    which prints out

    Array
    (
        [average] => 0
        [reviewsCount] => 0
        [category] => any
    )

    Any thoughts?

    You can also submit the form via ajax by using Malsup’s jQuery Form Plugin along with the jQuery validate code provided above.

    $('.rr_review_form').validate({
    	rules : {
    		input_stars : { selectStars : true }
    	},
    	submitHandler : function(form) {
    		// prepare the form for ajax submission
    		$(form).ajaxForm({
    			success : function() {},
    		});
    
    		// submit the form via ajax
    		$(form).ajaxSubmit();
    
    		// do your post submission tasks here, like hiding the form and displaying a success message.
    		// [NOTE: this really should be in "success" callback parameter, but for some reason that won't fire.]
    
    		// prevent the default submit
    		return false;
    	}
    });

    You can also submit the form via ajax by using Malsup’s jQuery Form Plugin along with the jQuery validate code provided above.

    $('.rr_review_form').validate({
    	rules : {
    		input_stars : { selectStars : true }
    	},
    	submitHandler : function(form) {
    		// prepare the form for ajax submission
    		$(form).ajaxForm({
    			success : function() {},
    		});
    
    		// submit the form via ajax
    		$(form).ajaxSubmit();
    
    		// do your post submission tasks here, like hiding the form and displaying a success message.
    		// [NOTE: this really should be in "success" callback parameter, but for some reason that won't fire.]
    
    		// prevent the default submit
    		return false;
    	}
    });

    You can also submit the form via ajax by using Malsup’s jQuery Form Plugin along with the jQuery validate code provided above.

    $('.rr_review_form').validate({
    	rules : {
    		input_stars : { selectStars : true }
    	},
    	submitHandler : function(form) {
    		// prepare the form for ajax submission
    		$(form).ajaxForm({
    			success : function() {},
    		});
    
    		// submit the form via ajax
    		$(form).ajaxSubmit();
    
    		// do your post submission tasks here, like hiding the form and displaying a success message.
    		// [NOTE: this really should be in "success" callback parameter, but for some reason that won't fire.]
    
    		// prevent the default submit
    		return false;
    	}
    });
    Thread Starter niemand_0

    (@niemand_0)

    After digging into this some I think I need some more guidance.

    What function in particular is returning an array of the reviews? Do I have access to this function in my template file?

    Ajax functionality needs to be added to the core of this plugin, but in the meantime this code will cause validation errors to display without reloading the page.

    The jQuery Validate plugin is needed for this code to work.

    // add "required" attribute to the inputs and textareas that should be required
    $('.rr_review_form .rr_required + .rr_form_input > input, .rr_review_form .rr_required + .rr_form_input > textarea').attr('required', 'required');
    
    // [IF USING STARS] add an input so stars can be validated
    $('.rr_stars_container').append('<input type="checkbox" name="input_stars" style="opacity: 0;">');
    
    // [IF USING STARS] add a function to remove error when stars are selected
    $('.rr_stars_container').click(function() {
    	if ($('.rr_stars_container').find('.glyphicon-star').length > 0) {
    		$('input[name="input_stars"]').removeClass('error');
    		$('label#input_stars-error').hide();
    	}
    });
    
    // [IF USING STARS] add a method to the validate plugin to check for a stars rating
    $.validator.addMethod("selectStars", function(value, element) {
    	return $('.rr_stars_container').find('.glyphicon-star').length > 0;
    }, "Please rate this product.");
    
    // validate the form
    $('.rr_review_form').validate({
    	// [IF USING STARS] add a method to the validate plugin to check for a stars rating
    	rules : {
    		input_stars : { selectStars : true }
    	}
    });
Viewing 15 replies - 1 through 15 (of 19 total)