Multiple if Statements issue - php

I am having a problem with multiple if statements. I am using && but it seems to only work for the first statement.
The code is like such:
global $post;
$args = array( 'post_id' => $post->ID );
$comment = get_comments( $args );
$user = wp_get_current_user();
if ( 3 <= count( $comment ) && $post->post_author == $user->ID) {
echo do_shortcode( '[button]' );
} else {
comment_form();
}
It basically stats that if there is less than 3 comments then show the comment form but if there is more than 3 and is the post author then show a button. The button shows but only if there are more than 3 comments. It doesn't check if it is only the post author or not, like I want it to.

What you are describing are two cases in which the button should be showed. So there have to be two ways to get into the if-block. You have to refactor your if-statement with the logical or-operator ||.
for example:
if($post->post_author == $user->ID || 3 <= count($comment)) {
echo do_shortcode( '[button]' );
} else {
comment_form();
}

I had to change the code like so to get it to work.
global $post,$current_user;
$args = array( 'post_id' => $post->ID );
$comment = get_comments( $args );
get_currentuserinfo();
if ($post->post_author == $current_user->ID ) {
echo do_shortcode( '[button]' );
} elseif ( 3 <= count( $comment ) ) {
// blank
} else {
comment_form();
}

Related

Adding search term to current query (Wordpress PHP)

Currently I've got this bit of code to do post-per-page filter to my woocommerce shop
function ps_selectbox() {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
ob_start();
echo '<div class="woocommerce-ordering">';
echo '<select onchange="if (this.value) window.location.href=this.value">';
$orderby_options = array(
'16' => '16 items per page',
'32' => '32 items per page',
'64' => '64 items per page'
);
foreach( $orderby_options as $value => $label ) {
echo "<option ".selected( $per_page, $value )." value='?perpage=$value'>$label</option>";
}
echo '</select>';
echo '</div>';
return ob_get_clean();
}
add_action( 'pre_get_posts', 'ps_pre_get_products_query' );
function ps_pre_get_products_query( $query ) {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
if( $query->is_main_query() && !is_admin() ) {
$query->set( 'posts_per_page', $per_page );
}
}
It will append "?perpage=$value" to the url and change number of items per page in the shop.
But when it comes to search result page, it will clear the current query and replace it with just "?perpage=$value"
EX: "https://aaa.com/?s=C21&post_type=product" to "https://aaa.com/?perpage=16"
If I manually change the url to be ""https://aaa.com/?s=C21&post_type=product&perpage=32" it works.
I tried add_query_arg( $args ) and a condition check but it doesn't seem to work.
EX:
function ps_pre_get_products_query( $query ) {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
if (is_search()) {
add_query_arg( $per_page );
}
elseif( $query->is_main_query() && !is_admin() ) {
$query->set( 'posts_per_page', $per_page );
}
}
But it doesn't seem to work.
Is there anyway I can keep this perpage dropdown feature in search result page?
Thanks in advance. Cheers.

How do I hide/show certain elements/divs if URL contains a specific string?

I have a large set of pages (page1/) that need to show a certain navigation bar and a second large set of pages (page2/) that need to show a different navigation bar in its place.
I have tried various jQuery to identify if the URL contains a certain word and, if it does, show the corresponding navigation bar - but with no success.
Below is the jQuery I've tried.
<script>
if(strpos($url ,'page1/') !== FALSE) {
echo '#page1-navigation-bar';
}
if(strpos($url ,'page2/') !== FALSE){
echo '#page2-navigation-bar';
}
</script>
This has no effect and I'm not sure whether this is because the coding is wrong, I've inputted it in the wrong place, I need to do something else as well as this coding or a combination of everything plus more.
Also not sure if this will hide one when showing the other?
Please help by stating any code needed and where exactly this needs inputting.
Thanks,
Michael
Maybe try like this in PHP:
<?php
$page = array("page1/" /*, "page2/", "page3/", "all other pages" */ );
if(in_array(strpos($url, $page))
{echo '#page1-navigation-bar';}
else
{echo '#page2-navigation-bar';}
?>
There are a couple ways you can go about this. You can do as WebSon has suggested but with a different approach, you can do it via page templates, or you can do it with custom post metas. Now note, while you're doing something with "ID's," I suggest you change the navigation displayed using wp_nav_menu().
One method with a suggested conditional, instead of ID's.
<?php
$first_array = [ 'page1', 'page3', 'page5' ];
$second_array = [ 'page2', 'page4', 'page6' ];
$wp_nav_args = [ //your default args ];
if ( is_page($first_array ) ) {
// Change the entire array or parts of it.
$wp_nav_args = [];
}
elseif ( is_page($second_array) ) {
// Change the entire array or parts of it.
$wp_nav_args = [];
}
wp_nav_menu($wp_nav_args);
Page Templates
<?php
$wp_nav_args = [ //your default args ];
if ( is_page_template('template-menu-a') ) {
// Change the entire array or parts of it.
$wp_nav_args = [];
}
elseif ( is_page_template('template-menu-b') {
// Change the entire array or parts of it.
$wp_nav_args = [];
}
wp_nav_menu($wp_nav_args);
More complicated way, which doesn't include templates, yet is extendable.
Just wanted to share this way, as you can use this for a few other things as well, by creating custom post metas. This example shows a checkbox in the Update/Publish box.
<?php
function add_custom_meta_field() {
$post_id = get_the_ID();
if ( get_post_type( $post_id ) !== 'page' ) {
return;
}
$value = get_post_meta( $post_id, '_navigation_b', true );
?>
<div class="misc-pub-section misc-pub-section-last">
<input type="checkbox" value="1" <?php checked( $value, true, true ); ?> name="_navigation_b" id="_navigation_b" /><label for="_navigation_b"><?php _e( 'Add To Your Calendar Icons', 'navy' ); ?></label>
</div>
<?php
}
add_action( 'post_submitbox_misc_actions', 'add_custom_meta_field' );
function save_custom_meta_field() {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST['_navigation_b'] ) ) {
update_post_meta( $post_id, '_navigation_b', $_POST['_navigation_b'] );
} else {
delete_post_meta( $post_id, '_navigation_b' );
}
}
add_action( 'save_post', 'save_custom_meta_field' );

Gravity Forms - Prevent Multiple Form Entries Same User ID

We are using Gravity Forms and need to prevent the user from accessing the form again after submission. This is the code we're using in the 'application.php' template:
<?php
if (get_current_user_id()) {
//lookup current application:
$query = "SELECT * FROM dg_application where user_id = " . get_current_user_id();
$dg_application = $wpdb->get_row( $query, ARRAY_A );
if ($dg_application["user_id"] && $dg_application["status"] == "Completed") {
// custom field
echo get_post_meta($post->ID, "form_complete_message", true);
} else {
//let Gravity Forms/WP do thier jobs
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
// End of the loop.
endwhile;
}
} else {
echo "Sorry. You must be logged in to view this form.";
}
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
But it's not working and not displaying the message like it should. The database name is correct. We are using a custom database. Any ideas why it might not be showing?
I have tried many ways. not sure this is the better way or not but it's working well for me.
This code will prevent form access for the current user if they already filled the form.
add_filter( 'gform_get_form_filter_3', 'custom_schedule', 10, 2 );
function custom_schedule( $form_string, $form ) {
$current_user = wp_get_current_user();
$search_criteria = array(
'status' => 'active',
'field_filters' => array( //which fields to search
array(
'key' => 'created_by', 'value' => $current_user->ID, //Current logged in user
)
)
);
$form_id = 3;
$entry = GFAPI::get_entries($form_id,$search_criteria);
if ( !empty($entry) ) {
$form_string = '<p>Sorry, you have reached the submission limit for this form.</p>';
}
return $form_string;
}

Looping through ACF in custom post types using functions in php to display the values in page. foreach quits working when placed in the function

I created this code to display the location from ACF that matches part of the URL and it works as expected.
<?php
$myurl= htmlspecialchars($_SERVER["REQUEST_URI"]);
$myexplodes = ( explode ('/', $myurl) );
$posts = get_posts(array(
'post_type' => 'my_vars',
));
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
?>
But when I try to place this code into a function nothing is displayed when I call it.
function local (){
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
}
I suspected that it is a scope problem with the vars but I have tried to make the vars global but had no luck.
The first one probably works because $post is a WordPress global variable, so I advise to use another variable name in your foreach.
For the thing that you want to do you should use also the post id in the get_field function call:
$value = get_field( "location", $article->ID );

Custom Fields not showing in custom post type post

I have a custom post type named "Designer" Each posts will be using different unique Advanced Custom Fields as each posts has unique templates.With the below code I am able to give rules for each posts in Designer post type and save but the custom fields are not displaying on post edit pages on backend.
Normally this code should ork but no idea what happend to the code
Please Help.
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices )
{
$choices['Custom Post types']['cpt_parent'] = 'Custom post type parent';
return $choices;
}
add_filter('acf/location/rule_values/cpt_parent', 'acf_location_rules_values_cpt_parent');
function acf_location_rules_values_cpt_parent( $choices )
{
$args = array(
'hierarchical' => true,
'_builtin' => false
);
$posttypes = get_post_types( $args );
if( $posttypes )
{
foreach( $posttypes as $posttype ):
if( $posttype != 'acf' ):
$args = array(
'post_type' => 'designer',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$customposts = get_posts( $args );
if ( $customposts ) {
foreach( $customposts as $custompost ){
$choices[ $custompost->ID] = $custompost->post_title;
}
}
endif;
endforeach;
}
return $choices;
}
//MATCH THE RULE
add_filter('acf/location/rule_match/cpt_parent', 'acf_location_rules_match_cpt_parent', 10, 3);
function acf_location_rules_match_cpt_parent( $match, $rule, $options )
{
global $post;
$selected_post = (int) $rule['value'];
// post parent
$post_parent = $post->post_parent;
if( $options['page_parent'] ) {
$post_parent = $options['page_parent'];
}
if ($rule['operator'] == "=="){
$match = ( $post_parent == $selected_post );
}
elseif ($rule['operator'] != "!="){
$match = ( $post_parent != $selected_post );
}
return $match;
}
Your Artist Collection field group is set to only appear on one post, the post Designer Post 1 which is a Designer Post type.
I don't understand what all the code is for? Just create a different field group for each post that needs a different field group and a separate rule for each.
Ok sorry I understand the issue now and I have recreated the issue on my local install.
On the line of code below you are looking for the post_parent but I think you should be looking for the ID.
I changed this:
$post_parent = $post->post_parent;
to this:
$post_parent = $post->ID;
and it's working for me.
If I understand your problem correctly, in wp-admin post edit page click on screen options on the upper right corner. In the menu that appears make sure the Custom fields is selected. This will make the custom fields appear for edit.

Categories